我正在尝试将图像文件的完整路径作为字符串,但它不起作用。我只是得到了一个像“content:/ external / media / images / 1”的结果。这绝对不是正确的道路。如何获得正确的路径,包括文件扩展名?
这是我到目前为止所尝试的内容:
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnGetImage:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select A Picture"),
PHOTO_GALLERY);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case PHOTO_GALLERY:
if (resultCode == RESULT_OK) {
File file = new File(data.getDataString());
String imagePath = file.getAbsolutePath();
break;
}
}
}
答案 0 :(得分:1)
试试这段代码:
case R.id.btnGetImage:
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
break;
和
@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]);
picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
picturePath是您需要的路径...
答案 1 :(得分:0)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case PHOTO_GALLERY:
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getRealPathFromURI(selectedImageUri);
File file = new File(selectedImagePath );
break;
}
}
}
答案 2 :(得分:0)
您已在路径中定义了正确的filename
,该路径必须存在于您的SD卡中,而从sdcard获取,
String filepath = Environment.getExternalStorageDirectory()+“/ foldername /”+ test.png;
我闻到你试图做this之类的事情。