在android studio中检索图像路径

时间:2015-03-17 20:25:36

标签: android android-intent android-activity

我正在开发一个Android应用程序。它具有捕获图像应显示在屏幕上的功能,我需要获取该图像路径,以便我可以将该图像发送到服务器。

这是我的代码:

 private void takePhoto(View v) {

        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
        photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"pictures.jpg");


        imageUri=Uri.fromFile(photo);
       intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        startActivityForResult(intent,TAKE_PICTURE);
    }


public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==TAKE_PICTURE)
        {
        if(resultCode==Activity.RESULT_OK) {


            Uri selectedImage = imageUri;
            getActivity().getContentResolver().notifyChange(selectedImage, null);
            ImageView imageview = (ImageView) getActivity().findViewById(R.id.imageView);
            ContentResolver cr = getActivity().getContentResolver();

        try {
                    //Adding Image Attachment to email
           String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getActivity().getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();
            columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            attachmentFile = cursor.getString(columnIndex);
            Log.e("Attachment Path:", attachmentFile);
            URI = Uri.parse("file://" + attachmentFile);
            cursor.close();

                    bitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage);
                    imageview.setImageBitmap(bitmap);
                    Toast.makeText(getActivity(), "PhotoShoot Complete", Toast.LENGTH_LONG).show();
        }
  catch (Exception e) {
                Toast.makeText(getActivity(),"Could not Complete Please Try Again.",Toast.LENGTH_SHORT);
                Log.e("ERROR", e.toString());
            }

目前路径的格式为:

file:///storage/emulated/0/Pictures/pictures.jpg

我想要有格式的路径: content://media/external/images/media/34826

当我尝试更换

Uri selectedImage = imageUri;

通过

Uri selectedImage = data.getData();

对于所需内容,“data”包含空值。

0 个答案:

没有答案