development

파일 경로에서 비트 맵 / 드로어 블 만들기

big-blog 2020. 10. 25. 12:37
반응형

파일 경로에서 비트 맵 / 드로어 블 만들기


기존 파일 경로에서 Bitmap 또는 Drawable을 만들려고합니다.

String path = intent.getStringExtra("FilePath");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPreferredConfig = Bitmap.Config.ARGB_8888;

mImg.setImageBitmap(BitmapFactory.decodeFile(path));
// mImg.setImageBitmap(BitmapFactory.decodeFile(path, option));
// mImg.setImageDrawable(Drawable.createFromPath(path));
mImg.setVisibility(View.VISIBLE);
mText.setText(path);

그러나 setImageBitmap(), setImageDrawable()경로에서 이미지를 표시하지 않습니다. 경로를 인쇄했는데 mText다음과 같이 보입니다./storage/sdcard0/DCIM/100LGDSC/CAM00001.jpg

내가 도대체 ​​뭘 잘못하고있는 겁니까? 누구든지 나를 도울 수 있습니까?


파일 경로에서 비트 맵 만들기 :

File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);

비트 맵을 부모의 높이와 너비로 크기를 조정하려면 Bitmap.createScaledBitmap함수 를 사용하십시오 .

잘못된 파일 경로를 제공하고 있다고 생각합니다. :) 도움이 되었기를 바랍니다.


나를 위해 작동합니다.

File imgFile = new  File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    //Drawable d = new BitmapDrawable(getResources(), myBitmap);
    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}

편집하다:

위의 하드 코딩 된 sdcard 디렉토리가 귀하의 경우에 작동하지 않는 경우 sdcard 경로를 가져올 수 있습니다.

String sdcardPath = Environment.getExternalStorageDirectory().toString();
File imgFile = new  File(sdcardPath);

여기에 해결책이 있습니다.

Bitmap bitmap = BitmapFactory.decodeFile(filePath);

글쎄, 정적을 사용하는 Drawable.createFromPath(String pathName)것은 스스로 디코딩하는 것보다 나에게 조금 더 간단 해 보인다 ... :-)

당신 mImg이 간단한 ImageView경우 필요하지도 않고 mImg.setImageUri(Uri uri)직접 사용하십시오 .


static ArrayList< Drawable>  d;
d = new ArrayList<Drawable>();
for(int i=0;i<MainActivity.FilePathStrings1.size();i++) {
  myDrawable =  Drawable.createFromPath(MainActivity.FilePathStrings1.get(i));
  d.add(myDrawable);
}

경로를 통해 드로어 블에 액세스 할 수 없으므로 드로어 블과 함께 사람이 읽을 수있는 인터페이스를 원하면 프로그래밍 방식으로 빌드 할 수 있습니다.

클래스 어딘가에 HashMap을 선언하십시오.

private static HashMap<String, Integer> images = null;

//Then initialize it in your constructor:

public myClass() {
  if (images == null) {
    images = new HashMap<String, Integer>();
    images.put("Human1Arm", R.drawable.human_one_arm);
    // for all your images - don't worry, this is really fast and will only happen once
  }
}

이제 액세스 할 수 있습니다.

String drawable = "wrench";
// fill in this value however you want, but in the end you want Human1Arm etc
// access is fast and easy:
Bitmap wrench = BitmapFactory.decodeResource(getResources(), images.get(drawable));
canvas.drawColor(Color .BLACK);
Log.d("OLOLOLO",Integer.toString(wrench.getHeight()));
canvas.drawBitmap(wrench, left, top, null);

참고 URL : https://stackoverflow.com/questions/16804404/create-a-bitmap-drawable-from-file-path

반응형