从图库中选择图片时,uri为:/ storage / emulated / 0 /图片/ 图片:49769
我需要:/storage/emulated/0/Pictures/image.jpg
如何获取图像的实际名称?
我现在有这个:
if(requestCode==REQ_CODE_PICTURE && resultCode==RESULT_OK && data != null && data.getData() != null) {
fileUri = data.getData();
imageView.setImageURI(fileUri);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + fileUri.getPathSegments().get(fileUri.getPathSegments().size()-1));
Uri contentUri = Uri.parse(f.getAbsolutePath());
//uriToUpload = contentUri.toString();
uriToUpload = getRealPathFromURI(contentUri);
Log.i("AAAAAAA", uriToUpload);
public String getRealPathFromURI(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
这是cursor.moveToFirst()中的错误:
12-19 11:39:16.469 5096-5096 / com.example.formador.a42_camera E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.formador.a42_camera,PID:5096 java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 234,result = -1,data = Intent {dat = content://com.android.providers.media.documents/document/image:49769 flg = {0x1}} to activity {com.example.formador.a42_camera / com.example.formador.a42_camera.MainActivity}:java.lang.NullPointerException:尝试调用接口方法'int android.database.Cursor.getColumnIndexOrThrow(java.lang .string)'在空对象引用上 在android.app.ActivityThread.deliverResults(ActivityThread.java:3798) 在android.app.ActivityThread.handleSendResult(ActivityThread.java:3841) 在android.app.ActivityThread.access $ 1400(ActivityThread.java:154) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1440) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:234) 在android.app.ActivityThread.main(ActivityThread.java:5526) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' 在com.example.formador.a42_camera.MainActivity.getRealPathFromURI(MainActivity.java:163) at com.example.formador.a42_camera.MainActivity.onActivityResult(MainActivity.java:147) 在android.app.Activity.dispatchActivityResult(Activity.java:6490) 在android.app.ActivityThread.deliverResults(ActivityThread.java:3794) 在android.app.ActivityThread.handleSendResult(ActivityThread.java:3841) 在android.app.ActivityThread.access $ 1400(ActivityThread.java:154) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1440) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:234) 在android.app.ActivityThread.main(ActivityThread.java:5526) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
从你的uri获取真正的路径
public String getRealPathFromURI(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
答案 1 :(得分:0)
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.pickiimagefromgallery_android_examples.com.MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Select Image" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends Activity {
private static int IMG_RESULT = 1;
String ImageDecode;
ImageView imageViewLoad;
Button LoadImage;
Intent intent;
String[] FILE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageViewLoad = (ImageView) findViewById(R.id.imageView1);
LoadImage = (Button) findViewById(R.id.button1);
LoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMG_RESULT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == IMG_RESULT && resultCode == RESULT_OK
&& null != data) {
Uri URI = data.getData();
String[] FILE = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(URI,
FILE, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(FILE[0]);
ImageDecode = cursor.getString(columnIndex);
cursor.close();
Log.i("ImageDecode", "+++" + ImageDecode);
imageViewLoad.setImageBitmap(BitmapFactory
.decodeFile(ImageDecode));
}
} catch (Exception e) {
Toast.makeText(this, "Please try again", Toast.LENGTH_LONG)
.show();
}
}
}
<强>的Manifest.xml:强>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />