我想浏览一张图片,然后在我的编辑文本中显示我的图片的路径。它一直在工作,直到我按下我希望该窗口的图像然后我得到下一个错误:
12-24 14:18:58.377: E/AndroidRuntime(5606): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/72 }} to activity {ianco.test.andrei/ianco.test.andrei.BrowsePicture}: java.lang.NullPointerException
12-24 14:19:40.537: E/AndroidRuntime(5892): FATAL EXCEPTION: main
12-24 14:19:40.537: E/AndroidRuntime(5892): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/73 }} to activity {ianco.test.andrei/ianco.test.andrei.BrowsePicture}: java.lang.NullPointerException
12-24 14:19:40.537: E/AndroidRuntime(5892): at android.app.ActivityThread.deliverResults(ActivityThread.java:2553)
12-24 14:19:40.537: E/AndroidRuntime(5892): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
12-24 14:19:40.537: E/AndroidRuntime(5892): at android.app.ActivityThread.access$2000(ActivityThread.java:121)
12-24 14:19:40.537: E/AndroidRuntime(5892): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
12-24 14:19:40.537: E/AndroidRuntime(5892): at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 14:19:40.537: E/AndroidRuntime(5892): at android.os.Looper.loop(Looper.java:130)
12-24 14:19:40.537: E/AndroidRuntime(5892): at android.app.ActivityThread.main(ActivityThread.java:3701)
12-24 14:19:40.537: E/AndroidRuntime(5892): at java.lang.reflect.Method.invokeNative(Native Method)
12-24 14:19:40.537: E/AndroidRuntime(5892): at java.lang.reflect.Method.invoke(Method.java:507)
12-24 14:19:40.537: E/AndroidRuntime(5892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
12-24 14:19:40.537: E/AndroidRuntime(5892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
12-24 14:19:40.537: E/AndroidRuntime(5892): at dalvik.system.NativeStart.main(Native Method)
12-24 14:19:40.537: E/AndroidRuntime(5892): Caused by: java.lang.NullPointerException
12-24 14:19:40.537: E/AndroidRuntime(5892): at ianco.test.andrei.BrowsePicture.onActivityResult(BrowsePicture.java:57)
12-24 14:19:40.537: E/AndroidRuntime(5892): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
12-24 14:19:40.537: E/AndroidRuntime(5892): at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)
12-24 14:19:40.537: E/AndroidRuntime(5892): ... 11 more
我的.java是:`package ianco.test.andrei;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.util.Log;
public class BrowsePicture extends Activity {
//YOU CAN EDIT THIS TO WHATEVER YOU WANT
private static final int SELECT_PICTURE = 1;
private String filePath;
public EditText myText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button sButton = (Button) findViewById(R.id.button1);
sButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// in onCreate or any event where your want the user to
// select a file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
});
}
//UPDATED
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
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);
Log.i("the path of image is", filePath);
myText.setText(filePath);
cursor.close();
}
} }
}`
答案 0 :(得分:1)
将其放入点击方法中:
File dir = new File(Environment.getExternalStorageDirectory().toString() + "/sdcard/yourfolder");
Log.d("File path ",dir.getPath());
String dirPath = dir.getAbsolutePath();
if(dir.exists()&&dir.isDirectory()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
// tells your intent to get the contents
// opens the URI for your image directory on your sdcard
//its upto you what data you want image or video.
intent.setType("image/*");
// intent.setType("video/*");
intent.setData(Uri.fromFile(dir));
// intent.setType("media/*");
// intent.
startActivityForResult(intent, 1);
} else {
Toast.makeText(this, "No file exist to show", Toast.LENGTH_SHORT).show();
}
这是您的ActivityResult方法:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (data == null) {
Toast.makeText(this, "No image selected", Toast.LENGTH_SHORT).show();
//finish();
} else {
Uri selectedImageUri = data.getData();
//MEDIA GALLERY
String selectedImagePath = selectedImageUri.getPath();
if (selectedImagePath != null) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(selectedImageUri);
startActivity(intent);
} else {
Toast.makeText(this, "Image path not correct", Toast.LENGTH_SHORT).show();
}
}
}
}