简体   繁体   English

如何在Android中从内存中选择图像

[英]How to select image from memory in Android

This is my code:这是我的代码:

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



   public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (resultCode == RESULT_OK) {

                if (requestCode == SELECT_PICTURE) {

                    //copy selected image to temp directory
                    Uri selectedImageUri = data.getData();

                    File src =  new File(getRealPathFromURI(selectedImageUri));
                    File dst =  new File(pathstring) ;                  

                     try {
                         copy ( src,   dst) ;

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 


            }
....
}

public String getRealPathFromURI(Uri contentUri) {

            // can post image
            String [] proj={MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery( contentUri,
                            proj, // Which columns to return
                            null,       // WHERE clause; which rows to return (all rows)
                            null,       // WHERE clause selection arguments (none)
                            null); // Order-by clause (ascending by name)
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();

            return cursor.getString(column_index);
    }

I get an error on Android 6.0.1 but it works on a device with Android 4.4.2我在 Android 6.0.1 上遇到错误,但它适用于 Android 4.4.2 的设备

How can i get an existing image accross all versions?如何获得所有版本的现有图像?

You are getting an error on Android 6.0.1 because Marshmallow version requires to follow permission model.您在 Android 6.0.1 上遇到错误,因为 Marshmallow 版本需要遵循权限模型。

You should be checking if the user has granted permission of external storage by using:您应该使用以下方法检查用户是否已授予外部存储权限:

if (Build.VERSION.SDK_INT >= 23) {
  if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");

        //Start doing your stuff to get image from external storage
        Intent in = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult( in, SELECT_PICTURE);

  }else{

  //If not, you need to ask the user to grant your app a permission:
   ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
  }

}

Make sure that your activity implements OnRequestPermissionResult确保您的活动实现 OnRequestPermissionResult

So if user will approve the permission,following callback will help you to move forward with your stuffs因此,如果用户批准该权限,则以下回调将帮助您继续处理您的事情

 @Override
 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
     super.onRequestPermissionsResult(requestCode, permissions, grantResults);
     if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
     Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);

      //Start doing your stuff to get image from external storage
        Intent in = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult( in, SELECT_PICTURE);
   }
}

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

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