안드로이드 스튜디오 이미지 파일 경로 - andeuloideu seutyudio imiji pail gyeonglo

이전에 이미지 경로를 읽어오는 코드는 3001ssw.tistory.com/195 링크를 참고해주시면 됩니다.

내 이미지의 경로를 이용하여 이미지 뷰에 이미지를 표시하는 방법에 대해 알아보겠습니다.

우선 매니페스트에 아래와 같이 권한을 요청합니다.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

그리고 activity_main.xml에 아래와 같이 이미지뷰를 넣어줍니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/fileList"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/imgView"/>

</LinearLayout>

MainActivity.java에 아래와 같이 입력합니다.

public class MainActivity extends AppCompatActivity {

    private ImageView m_imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 경로 얻어오는 코드 중략..
        m_imageView = (ImageView) findViewById(R.id.imgView);
        m_list = findViewById(R.id.fileList);
        m_list.setAdapter(adapter);
        m_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String strImg = m_fileList.get(position);

                Toast.makeText(getApplicationContext(), strImg, Toast.LENGTH_SHORT).show();

                File file = new File(strImg);
                boolean bExist = file.exists();
                if (bExist) {
                    Bitmap myBitmap = BitmapFactory.decodeFile(strImg);
                    m_imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    m_imageView.setImageBitmap(myBitmap);
                }
            }
        });

    }

이렇게 하고나서도 이미지 표시가 안된다면 안드로이드 10부터 바뀐 저장소 정책때문일 수도 있습니다.

매니페스트에 application에 requestLegacyExternalStorage를 추가해줍니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.snj.filetest">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <!-- 권한 추가 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 권한 추가 -->

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.FileTest"
        android:requestLegacyExternalStorage="true"> <!-- Scoped Storage -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

자세한 내용은 codechacha.com/ko/android-q-scoped-storage/를 참고해주시면 됩니다.

실행한다면 아래와 같이 표시 가능합니다.

안드로이드 스튜디오 이미지 파일 경로 - andeuloideu seutyudio imiji pail gyeonglo

[안드로이드] 이미지 경로 이미지뷰에 출력하기

SD-Card에서 이미지의 경로를 받아와 ImageView에 등록하는것을 원하는거라면 Bitmap을 써서

File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

이런식으로하시면됩니다.

물론 안드로이드 매니페스트 파일에 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />를 등록하셔야합니다.

[ 출처 : http://hashcode.co.kr/questions/1072/%EC%9D%B4%EB%AF%B8%EC%A7%80%EB%B7%B0%EC%97%90-%ED%8C%8C%EC%9D%BC%EC%9D%98-%EA%B2%BD%EB%A1%9C%EB%A1%9C-%EC%9D%B4%EB%AF%B8%EC%A7%80%EB%A5%BC-%EB%84%A3%EC%96%B4%EC%A3%BC%EB%8A%94-%EB%B2%95 ]

안드로이드 스튜디오 이미지 파일 경로 - andeuloideu seutyudio imiji pail gyeonglo
라이브러리 추가

import android.net.Uri;
import java.io.File;           

//=========================================

1. URI 이용방법

 try { 

    File files = new File("파일 실제 경로");

    if(files.exists()==true) {

           Uri uri = Uri.parse("파일 실제 경로");
           imagview1.setImageURI(uri);      
      }

   }catch (Exception e){ 
       e.printStackTrace(); 
   } 

//==========================================

2. Bitmap  이용방법

 try { 

    File files = new File("파일 실제 경로");

    if(files.exists()==true) {

          Bitmap myBitmap = BitmapFactory.decodeFile(files.getAbsolutePath());
           imagview1.setImageBitmap(myBitmap);
      }

   }catch (Exception e){ 
       e.printStackTrace(); 
   }