Android - Image Picker, Wrong Image

后端 未结 5 1140
情书的邮戳
情书的邮戳 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:37

    This is the code to open gallery. However this the same what you have done. Also see the onActivityResult code which I used to retrive the selected image.

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"),  
                           PHOTO_GALLERY);
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case PHOTO_GALLERY:
            if (resultCode == RESULT_OK) {
                Uri selectedImageUri = Uri.parse(data.getDataString());
                try {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(  
                                       getApplicationContext().getContentResolver(),   
                                       selectedImageUri);
    
                    this.postImagePreview.setImageBitmap( bitmap);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-19 05:42

    private static int RESULT_LOAD_IMAGE = 1;

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

    OnActivity Result

      @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };
                    Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    ImageView imageView = (ImageView) findViewById(R.id.imgView);
                    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                }
            }
    
    0 讨论(0)
  • 2021-02-19 05:42

    try this one

     //Put this code on some event 
    
       Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    
     startActivityForResult(intent, REQUEST_CODE);
    
               // When above event fire then its comes to this 
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                  if (resultCode==RESULT_OK && requestCode==1){
    
                      Uri selectedImage = data.getData();
                        String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
                        Cursor cursor = getContentResolver().query(selectedImage,
                                filePathColumn, null, null, null);
                        cursor.moveToFirst();
                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                        filePath = cursor.getString(columnIndex);
                        cursor.close();
    
                                // Use it as per recruitment 
                        actualBitmap =BitmapFactory.decodeFile(filePath);
    
                  }
        }
    
    0 讨论(0)
  • 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
                }
        }
    }
    
    }
    
    0 讨论(0)
  • 2021-02-19 06:00

    Sometimes the thumbnails in the gallery app can be outdated and show thumbnails for a different image. This can happen when the image ids are reused, for example when an image gets deleted and a new one is added using the same id.

    Manage Apps > Gallery > Clear Data can fix this problem then.

    0 讨论(0)
提交回复
热议问题