Android - Image Picker, Wrong Image

后端 未结 5 1116
情书的邮戳
情书的邮戳 2021-02-19 05:12

I am starting a request for an image pick:

Intent intent = new Intent();
intent.setType( \"image/*\" );
intent.setAction( Intent.ACTION_GET_CONTENT );
startActiv         


        
5条回答
  •  无人及你
    2021-02-19 05:48

    Try this,

    public class SelectPhotoActivity extends Activity {
    
    private static final int SELECT_PICTURE = 1;
    private String selectedImagePath="";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, SELECT_PICTURE); 
    }
    
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE)
            {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
               // here you can set the image
                }
        }
    }
    
    }
    

提交回复
热议问题