Create a Bitmap/Drawable from file path

后端 未结 6 1068
故里飘歌
故里飘歌 2020-11-28 06:56

I\'m trying to create a Bitmap or Drawable from existing file path.

String path = intent.getStringExtra(\"FilePath\");
BitmapFactory.Options option = new Bit         


        
相关标签:
6条回答
  • 2020-11-28 07:37

    you can't access your drawables via a path, so if you want a human readable interface with your drawables that you can build programatically.

    declare a HashMap somewhere in your class:

    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
      }
    }
    

    Now for access -

    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);
    
    0 讨论(0)
  • 2020-11-28 07:38

    It works for me:

    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);
    
    }
    

    Edit:

    If above hard-coded sdcard directory is not working in your case, you can fetch the sdcard path:

    String sdcardPath = Environment.getExternalStorageDirectory().toString();
    File imgFile = new  File(sdcardPath);
    
    0 讨论(0)
  • 2020-11-28 07:39

    Create bitmap from file path:

    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);
    

    If you want to scale the bitmap to the parent's height and width then use Bitmap.createScaledBitmap function.

    I think you are giving the wrong file path. :) Hope this helps.

    0 讨论(0)
  • 2020-11-28 07:54

    here is a solution:

    Bitmap bitmap = BitmapFactory.decodeFile(filePath);
    
    0 讨论(0)
  • 2020-11-28 07:57

    Well, using the static Drawable.createFromPath(String pathName) seems a bit more straightforward to me than decoding it yourself... :-)

    If your mImg is a simple ImageView, you don't even need it, use mImg.setImageUri(Uri uri) directly.

    0 讨论(0)
  • 2020-11-28 07:57
    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);
    }
    
    0 讨论(0)
提交回复
热议问题