Get/pick an image from Android's built-in Gallery app programmatically

前端 未结 19 1833
终归单人心
终归单人心 2020-11-22 00:49

I am trying to open an image / picture in the Gallery built-in app from inside my application.

I have a URI of the picture (the picture is located on the SD card).

19条回答
  •  生来不讨喜
    2020-11-22 01:22

    package com.ImageConvertingDemo;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.EditText;
    import android.widget.ImageView;
    
    public class MyActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            EditText tv = (EditText)findViewById(R.id.EditText01);
            ImageView iv = (ImageView)findViewById(R.id.ImageView01);
            FileInputStream in;
            BufferedInputStream buf;
                try 
                {
                    in = new FileInputStream("/sdcard/smooth.png");
                    buf = new BufferedInputStream(in,1070);
                    System.out.println("1.................."+buf);
                    byte[] bMapArray= new byte[buf.available()];
                    tv.setText(bMapArray.toString());
                    buf.read(bMapArray);
                    Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
    
                    /*for (int i = 0; i < bMapArray.length; i++) 
                    {
                    System.out.print("bytearray"+bMapArray[i]);
                    }*/
                    iv.setImageBitmap(bMap);
                    //tv.setText(bMapArray.toString());
                    //tv.setText(buf.toString());
                    if (in != null) 
                    {
                        in.close();
                    }
                    if (buf != null) 
                    {
                        buf.close();
                    }
    
                } 
                catch (Exception e) 
                {
                    Log.e("Error reading file", e.toString());
                }
        }
    }
    

提交回复
热议问题