获取Android上的图片文件路径 [英] Get file path of image on Android

查看:217
本文介绍了获取Android上的图片文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以拍照并上传的应用程式。上传需要照片的文件路径,但我无法获取。



这是我的代码:

  public void maakfoto(View v){

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,mImageCaptureUri);
startActivityForResult(cameraIntent,CAMERA_REQUEST);

}

protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == CAMERA_REQUEST& resultCode == RESULT_OK) {

位图照片=(位图)data.getExtras()。get(data);
imageView.setImageBitmap(photo);
knop.setVisibility(Button.VISIBLE);
System.out.println(mImageCaptureUri);
}
}

请帮助我获取文件路径。 p>

谢谢

解决方案

发布到Twitter需要图片的实际路径设备在发送请求中发送。我发现很难得到实际的路径,而且经常会得到错误的路径。



为了解决这个问题,一旦你有一个 Bitmap ,我使用它来从使用 getImageUri()获取URI。随后,我使用 tempUri Uri实例来获取设备上的实际路径。



生产代码并自然测试。 ; - )

  protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == CAMERA_REQUEST&& resultCode == RESULT_OK){
位图照片=(位图)data.getExtras()。get(data);
imageView.setImageBitmap(photo);
knop.setVisibility(Button.VISIBLE);


//调用从BITMAP获取URI的方法
Uri tempUri = getImageUri(getApplicationContext(),photo);

//获取实际路径的方法
File finalFile = new File(getRealPathFromURI(tempUri));

System.out.println(mImageCaptureUri);
}
}

public Uri getImageUri(Context inContext,Bitmap inImage){
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG,100,bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(),inImage,Title,null);
return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri){
Cursor cursor = getContentResolver()。query(uri,null,null,null,null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}


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 void maakfoto (View v) {

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  

            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
            knop.setVisibility(Button.VISIBLE);
            System.out.println(mImageCaptureUri);
        }  
    }

Please help me to get the file path.

Thank you

解决方案

Posting to Twitter needs the image's actual path on the device to be sent in the request to post. I was finding it mighty difficult to get the actual path and more often than not I would get the wrong path.

To counter that, once you have a Bitmap, I use that to get the URI from using the getImageUri(). Subsequently, I use the tempUri Uri instance to get the actual path as it is on the device.

This is production code and naturally tested. ;-)

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
        knop.setVisibility(Button.VISIBLE);


        // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
        Uri tempUri = getImageUri(getApplicationContext(), photo);

        // CALL THIS METHOD TO GET THE ACTUAL PATH
        File finalFile = new File(getRealPathFromURI(tempUri));

        System.out.println(mImageCaptureUri);
    }  
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
}

这篇关于获取Android上的图片文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆