Get file path of image on Android

后端 未结 8 1738
长发绾君心
长发绾君心 2020-11-30 09:48

I have an app that can make pictures and upload them. The upload requires the file path of the photo but I can\'t get it.

This is my code:

public voi         


        
相关标签:
8条回答
  • 2020-11-30 10:36

    Simple Pass Intent first

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

    And u will get picture path on u onActivityResult

    @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)
  • 2020-11-30 10:39

    To get the path of all images in android I am using following code

    public void allImages() 
    {
        ContentResolver cr = getContentResolver();
        Cursor cursor;
        Uri allimagessuri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Images.Media._ID + " != 0";
    
        cursor = cr.query(allsongsuri, STAR, selection, null, null);
    
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
    
                    String fullpath = cursor.getString(cursor
                            .getColumnIndex(MediaStore.Images.Media.DATA));
                    Log.i("Image path ", fullpath + "");
    
    
                } while (cursor.moveToNext());
            }
            cursor.close();
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题