通过从MediaStore.ACTION_IMAGE_CAPTURE中选取图像来创建文件 [英] Creating a file from picking an image from MediaStore.ACTION_IMAGE_CAPTURE
问题描述
意图pickIntent = new Intent();
pickIntent.setType(image / *);
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
字符串pickTitle =选择或拍摄新照片; //或者从strings.xml获取
Intent chooserIntent = Intent.createChooser(pickIntent,pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent [] {takePhotoIntent}
);
startActivityForResult(chooserIntent,SELECT_PICTURE);
在 onActivityResult
中我想创建一个从所选图像中新建文件
。这是我正在做的:
$ pre $ @ $ $ b $公共无效onActivityResult(int requestCode,int resultCode,意图数据) {
super.onActivityResult(requestCode,resultCode,data);
if(requestCode == 1&& resultCode == Activity.RESULT_OK){
if(data == null){
return;
}
Uri selectedImageUri = data.getData();
文件mediaFile =新文件(selectedImageUri.getPath()); $!
$ b if(!mediaFile.exists())
Log.d(File,File does not exist);
我检查 mediaFile.exists ()
返回false。我在这里做错了什么?
更新
well:
字符串fullPath = getRealPathFromURI(selectedImageUri);
用在这里输入链接描述问题:
public String getRealPathFromURI(Uri contentUri){
Cursor cursor = null;
尝试{
String [] proj = {MediaStore.Images.Media.DATA};
cursor = getContentResolver()。query(contentUri,proj,null,null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(column_index);
} catch(Exception e){
e.printStackTrace();
} finally {
if(cursor!= null){
cursor.close();
}
}
返回null;
}
但是,这个方法返回null。
<
public class RealPathUtil
{
@SuppressLint(NewApi)
public static String getRealPathFromURI_API19(Context context,Uri uri){
String filePath =;
字符串wholeID = DocumentsContract.getDocumentId(uri);
//在冒号处分割,在数组中使用第二项
String id = wholeID.split(:)[1];
String [] column = {MediaStore.Images.Media.DATA};
//其中id等于
String sel = MediaStore.Images.Media._ID +=?;
cursor cursor = context.getContentResolver()。query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column,sel,new String [] {id},null);
int columnIndex = cursor.getColumnIndex(column [0]);
if(cursor.moveToFirst()){
filePath = cursor.getString(columnIndex);
}
cursor.close();
返回filePath;
$ b @SuppressLint(NewApi)
public static String getRealPathFromURI_API11to18(Context context,Uri contentUri){
String [] proj = { MediaStore.Images.Media.DATA};
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri,proj,null,null,null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor!= null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
返回结果;
public static String getRealPathFromURI_BelowAPI11(Context context,Uri contentUri){
String [] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver()。query(contentUri,proj,null,null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(column_index);
$ / code $ / pre
< p>使用android的相对方法从uri获取图像路径api。
:检查您的手机的SDK版本
路径使用
int version = Build.VERSION.SDK_INT;
字符串fullPath;
if(version> = 19){
// call api 19 code here
fullPath = getRealPathFromURI_API19(this,uri);
} else if(version <= 18&& version> = 11){
// call api 11-18 code here
fullPath = getRealPathFromURI_API11to18(this,uri);
} else {
//在api下面调用11代码
fullPath = getRealPathFromURI_BelowAPI11(this,uri);
}
文件mediaFile = new File(fullPath);
参考这个
I'm allowing the user to pick an image from their photo gallery:
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra
(
Intent.EXTRA_INITIAL_INTENTS,
new Intent[] { takePhotoIntent }
);
startActivityForResult(chooserIntent, SELECT_PICTURE);
And in onActivityResult
I want to create a new File
from the selected image. Here is what I am doing:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
if (data == null) {
return;
}
Uri selectedImageUri = data.getData();
File mediaFile = new File(selectedImageUri.getPath());
if(!mediaFile.exists())
Log.d("File", "File doesn't exist");
}
}
My check mediaFile.exists()
is returning false. What am I doing wrong here?
Update
I tried doing this as well:
String fullPath = getRealPathFromURI(selectedImageUri);
with this method from enter link description here question:
public String getRealPathFromURI(Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
return null;
}
However, this method returns null.
Add this class to your project.
public class RealPathUtil {
@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
@SuppressLint("NewApi")
public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
String result = null;
CursorLoader cursorLoader = new CursorLoader(
context,
contentUri, proj, null, null, null);
Cursor cursor = cursorLoader.loadInBackground();
if(cursor != null){
int column_index =
cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(column_index);
}
return result;
}
public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
and get image path from uri using relative method of android api.
EDIT :: Check SDK Version of your phone
And get path using
int version = Build.VERSION.SDK_INT;
String fullPath;
if(version >= 19) {
//call api 19 code here
fullPath = getRealPathFromURI_API19(this, uri);
} else if(version <= 18 && version >= 11) {
// call api 11-18 code here
fullPath = getRealPathFromURI_API11to18(this, uri);
} else {
// call below api 11 code here
fullPath = getRealPathFromURI_BelowAPI11(this, uri);
}
File mediaFile = new File(fullPath);
Reference from this
这篇关于通过从MediaStore.ACTION_IMAGE_CAPTURE中选取图像来创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!