问题描述
我要上传图片,音频和视频文件从我的应用程序。如果我选择使用图片库(内容://媒体/外部/图像/媒体/ 3163
),它工作正常,但如果我选择使用文件管理器(文件:///storage/sdcard0/blue.jpg
),它不工作
I want to upload image, audio and video files from my app. If I select image using Gallery (content://media/external/images/media/3163
) it works fine, but if I select using File Manager (file:///storage/sdcard0/blue.jpg
) it is not working.
uri=intent.getData();
String[] filePathColumn = { MediaStore.Images.ImageColumns.DATA };
if (uri != null) {
Cursor cursor = RootActivity.rootContext.getContentResolver().query(Uri.parse(uri),filePathColumn, null, null, null);
}
///storage/sdcard0/blue.jpg ,任何一个可以帮助我
The cursor returns null if uri = file:///storage/sdcard0/blue.jpg
, can any one help me?
光标如果 在此先感谢 Thanks in advance 试用如下: 和您的活动添加两个方法 and add two methods in your activity 和添加此INT作为类成员 and add this int as class member 我希望这会帮助你。 I hope it will help you. 感谢。 这篇关于我要转换文件:///storage/sdcard0/blue.jpg内容://媒体/外部/图像/ android的媒体/ 3163的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! URI =文件返回null?
推荐答案
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String s1 = data.getDataString();
//String s1 = selectedImageUri.getPath();
Log.e("GetPath",s1);
Log.e("OK",""+selectedImageUri);
selectedImagePath = getPath(selectedImageUri);
if(selectedImagePath==null && s1 != null)
{
selectedImagePath = s1.replaceAll("file://","");
}
Intent intent = new Intent(this, PhotoEditorActivity.class);
intent.putExtra("path", selectedImagePath);
startActivity(intent);
finish();
}
}
}
//Return the path of the file.
public String getPath(Uri uri) {
try{
String[] projection = { MediaStore.Images.Media.DATA };
Log.e("OK 1",""+projection);
Cursor cursor = managedQuery(uri, projection, null, null, null);
Log.e("OK 2",""+cursor);
if(cursor==null)
{
return null;
}
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.e("OK 3",""+column_index);
cursor.moveToFirst();
Log.e("OK 4",""+cursor.getString(column_index));
return cursor.getString(column_index);
}
catch(Exception e)
{
Toast.makeText(PhotoActivity.this, "Image is too big in resolution please try again", 5).show();
return null;
}
}
private static final int SELECT_PICTURE = 1;