从图库中选择不在android中工作的图像

时间:2017-10-11 06:09:00

标签: java android image-gallery photo-gallery

我正在尝试从图库中选择要在图像视图中显示的图像,选择有效但图像不会出现在图像视图中,它仍为空白。

代码:

public class MainActivity extends AppCompatActivity {

    private static final int SELECTED_PICTURE=1;
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        iv=(ImageView)findViewById(R.id.imageView1);

        //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        //fab.setOnClickListener(new View.OnClickListener() {
            //@Override
            //public void onClick(View view) {
                //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        //.setAction("Action", null).show();
            //}
        }//);
    public void btnClick(View v){
        Intent i=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, SELECTED_PICTURE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case SELECTED_PICTURE:
                if(resultCode==RESULT_OK){
                    Uri uri=data.getData();
                    String[]projection={MediaStore.Images.Media.DATA};

                    Cursor cursor=getContentResolver().query(uri, projection, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex=cursor.getColumnIndex(projection[0]);
                    String filePath=cursor.getString(columnIndex);
                    cursor.close();

                    Bitmap yourSelectedImage=BitmapFactory.decodeFile(filePath);
                    Drawable d=new BitmapDrawable(yourSelectedImage);

                    iv.setBackground(d);

                }
                break;

            default:
                break;
        }
    }
}

我做错了什么?

4 个答案:

答案 0 :(得分:0)

尝试以下代码;

iv.setImageBitmap(yourSelectedImage);

而不是代码;

 Drawable d=new BitmapDrawable(yourSelectedImage);
 iv.setBackground(d);

答案 1 :(得分:0)

检查此示例 我只是试一试,它正在工作!

http://codetheory.in/android-pick-select-image-from-gallery-with-intents/

希望它有所帮助,祝你好运

答案 2 :(得分:0)

我曾经遇到过同样的问题,有时候Uridata.getData()返回的文件路径为空。因此,这会导致图像为null。继承人就是我所做的:

首先,我没有从Uri获取文件路径,但我使用Uri本身使用以下方法为我提供了图像

    private Bitmap getBitmapFromUri(Uri uri)throws IOException{
    BitmapFactory.Options options = new BitmapFactory.Options();
    InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(uri);
    return BitmapFactory.decodeStream(inputStream, null, options);
}

您可以在Options中传递null。该方法抛出IOException,所以一定要抓住它。 因此,当您想在图像视图中使用它时:

imageView.setImageBitmap(getBitmapFromUri(data.getData()));

还要记住如何调整图像大小,因为用户可以选择一个非常大的图像,从而占用内存。还要修改代码以进行大小调整!

答案 3 :(得分:0)

首先确保您在 manifest.xml 文件中声明了所有必要的权限。然后尝试一下。我已经编辑了你的代码 -

public class MainActivity extends AppCompatActivity {

    private static final int SELECTED_PICTURE=1;
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        iv=(ImageView)findViewById(R.id.imageView1);

        //FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        //fab.setOnClickListener(new View.OnClickListener() {
            //@Override
            //public void onClick(View view) {
                //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        //.setAction("Action", null).show();
            //}
        }//);
    public void btnClick(View v){
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECTED_PICTURE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case SELECTED_PICTURE:
                if(resultCode==RESULT_OK){
                    Uri pickedImage = data.getData();
            // Let's read picked image path using content resolver
            String[] filePath = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
            cursor.moveToFirst();
            String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
            cursor.close();
            iv. setImageBitmap(bitmap);

                }
                break;

            default:
                break;
        }
    }
}