繁体   English   中英

从图库中选择图像在Android中不起作用

[英]Selecting image from gallery not working in android

我正在尝试从图库中选择要在imageview中显示的图像,选择有效,但该图像未出现在imageview中,它保持空白。

码:

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

我究竟做错了什么?

尝试下面的代码;

iv.setImageBitmap(yourSelectedImage);

而不是下面的代码;

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

检查这个例子,我只是尝试它,它正在工作!

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

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

我曾经遇到过同样的问题,这是由Uri有时从data.getData()返回的文件路径为null引起的。 因此,这将导致图像为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()));

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

首先,请确保您对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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM