[android] 파일 경로에서 이미지보기를 표시 하시겠습니까?

리소스 ID가 아닌 파일 이름 만 사용하여 이미지를 표시해야합니다.

ImageView imgView = new ImageView(this);
imgView.setBackgroundResource(R.drawable.img1);

drawable 폴더에 이미지 img1이 있습니다. 파일에서 해당 이미지를 보여주고 싶습니다.

어떻게해야합니까?



답변

Labeeb은 리소스가 이미 리소스 폴더 안에있는 경우 경로를 사용하여 이미지를 설정 해야하는 이유에 대해 맞습니다.

이러한 종류의 경로는 이미지가 SD 카드에 저장된 경우에만 필요합니다.

그리고 아래 코드를 시도하여 SD-Card 안에 저장된 파일에서 비트 맵 이미지를 설정하십시오.

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" />


답변

나는 당신이 이것을 사용할 수 있다고 생각합니다

Bitmap bmImg = BitmapFactory.decodeFile("path of your img1");
imageView.setImageBitmap(bmImg);


답변

다음을 사용할 수도 있습니다.



    File imgFile = new  File(“filepath”);
    if(imgFile.exists())
    {
        ImageView myImage = new ImageView(this);
        myImage.setImageURI(Uri.fromFile(imgFile));

    }

이것은 암시 적으로 비트 맵 디코딩을 수행합니다.


답변

String path = Environment.getExternalStorageDirectory()+ "/Images/test.jpg";

File imgFile = new File(path);
if(imgFile.exists())
{
   Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
   ImageView imageView=(ImageView)findViewById(R.id.imageView);
  imageView.setImageBitmap(myBitmap);
}


답변

모든 답변은 구식입니다. 그러한 목적으로 피카소 를 사용하는 것이 가장 좋습니다 . 백그라운드 이미지 처리를 포함한 많은 기능이 있습니다.

사용하기가 매우 쉽다고 언급 했습니까?

Picasso.with(context).load(new File(...)).into(imageView);


답변

공식 사이트에서 : http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

ImageView image = (ImageView) findViewById(R.id.imagePreview);           
try {
    image.setImageBitmap(decodeSampledBitmap(picFilename));
} catch (Exception e) {
    e.printStackTrace();
}

방법은 다음과 같습니다.

    private int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    private Bitmap decodeSampledBitmap(String pathName,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(pathName, options);
    }

//I added this to have a good approximation of the screen size: 
    private Bitmap decodeSampledBitmap(String pathName) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        return decodeSampledBitmap(pathName, width, height);
    }   


답변

당신이 사용할 수있는:

ImageView imgView = new ImageView(this);
InputStream is = getClass().getResourceAsStream("/drawable/" + fileName);
imgView.setImageDrawable(Drawable.createFromStream(is, ""));