programing

비트맵/파일 경로에서 그리기 가능

minecode 2023. 1. 14. 09:49
반응형

비트맵/파일 경로에서 그리기 가능

기존 파일 경로에서 비트맵 또는 그리기 가능 파일을 만들려고 합니다.

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)직접적으로.

For Drawable -
Drawable drawable = Drawable.createFromPath(your path in string);
For Bitmap -
Bitmap bitmap = BitmapFactory.decodeFile(your path in string);

얼마나 간단한지 네가 좋아했으면 좋겠어

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

반응형