【问题标题】:Show a picture in the gallery on the app in Android在 Android 应用程序的图库中显示图片
【发布时间】:2016-08-12 09:09:38
【问题描述】:

我正在编写一个应用程序,其中用户将单击应用程序中的一个按钮,画廊将弹出,用户将从画廊中选择一个图像,并且所选图像应该显示在我的 imageView 中放置在屏幕上。当我单击按钮时,图库会弹出,我选择了一个图像,但由于某种原因,该图像没有显示在我的应用程序屏幕上。非常感谢您的帮助。我只添加了两个权限如下:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

这是我的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showPicture(View view) {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
            Uri photoUri = data.getData();
            Bitmap selectedImage = null;

            try{
                selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
            } catch (Exception e){
                e.printStackTrace();
            }
            ImageView imageView = (ImageView) findViewById(R.id.image);
            imageView.setImageBitmap(selectedImage);
        }
    }
}

编辑:我尝试了答案中给出的代码,但在堆栈跟踪中出现以下错误。为简洁起见,仅显示 RuntimeExceptions:

                                                                           java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/46 }} to activity {cs193a.stanford.edu.testproject/cs193a.stanford.edu.testproject.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/46 from pid=2754, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

                                                                            Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/46 from pid=2754, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

我的代码中唯一似乎有问题的代码行也是声明光标的那一行。非常感谢您的帮助

【问题讨论】:

    标签: android gallery multimedia


    【解决方案1】:

    打开相册

    Intent i = new Intent(
    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    
    startActivityForResult(i, RESULT_LOAD_IMAGE);
    

    要取回结果:

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent imageReturnedIntent) {
        // TODO Auto-generated method stub
          super.onActivityResult(requestCode, resultCode, imageReturnedIntent);   
    
            switch(requestCode) {  
            case RESULT_LOAD_IMAGE:  
                if(resultCode == RESULT_OK)  
                {  
                    Uri uri = imageReturnedIntent.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 image = BitmapFactory.decodeFile(filePath);
    
                    ImageView im = (ImageView)findViewById(R.id.imagev);
                   im.setImageBitmap(image);
                }  
            }  
        }
    

    编辑:请立即尝试

    【讨论】:

    • 我尝试了上面的代码,但是我的应用程序在我使用它时崩溃了。这行好像有问题:Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
    • 你在 Marshamllow 上运行吗?
    • 你实现了运行时权限模型吗?
    • 不,我正在使用 Android Studio 及其本机模拟器,不,除了我上面发布的内容之外,我没有实现任何其他功能。
    • 模拟器android api级别?
    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 2010-10-07
    • 1970-01-01
    相关资源
    最近更新 更多