bra*_*d J 8 android photo android-intent android-camera-intent
我已经阅读了很多关于这个问题的问题.有些类似于我所经历的; 我没有成功地尝试了答案.该代码适用于HTC Android 4.2,不适用于Nexus 7 Android 4.4.我已经修改了存储目录以在android 4.4上工作,所以这不是问题.
如果我使用相机意图永远不会返回
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
Run Code Online (Sandbox Code Playgroud)
如果我使用,它会返回
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);
Run Code Online (Sandbox Code Playgroud)
但它不保存文件.
这是完整的代码.调用意图
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(manager) != null)
{
try
{
final File photoFile = createImageFile();
if (photoFile != null)
{
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
//takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);
startActivityForResult(takePictureIntent, 1);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
文件名
private File createImageFile() throws IOException
{
if (mStorageDirectory == null)
{
createInitialStorageDirectory();
setupFolders();
mScrollList.notifyDataSetInvalidated();
}
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "slide_" + timeStamp;
File storageDir = new File(mStorageDirectory);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
// image.setWritable(true, false);
// Save a path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
Run Code Online (Sandbox Code Playgroud)
回调函数
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == -1)
{
}
}
Run Code Online (Sandbox Code Playgroud)
根目录加上保存目录
static String mBasePath = "/slides/";
private String getRootDirectory()
{
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
if (Build.VERSION.SDK_INT >= 19)
return mView.getContext().getFilesDir() + mBasePath;
else
return Environment.getExternalStorageDirectory() + "/Android/data/com.hdms.manager" + mBasePath;
//return Environment.getExternalStorageDirectory() + mBasePath;
}
return mBasePath;
}
Run Code Online (Sandbox Code Playgroud)
任何想法将不胜感激.
在我的情况下,它没有返回,因为输出文件在我尚未创建的文件夹中.
如果是其他人的情况,请在启动anintent之前执行此操作:
file.getParentFile().mkdirs();
Run Code Online (Sandbox Code Playgroud)
所以我找到了解决问题的方法.如果文件不存在,则Camera Activity将不会返回.我猜它在4.4上不起作用的原因是对文件系统的更改.我没有将图像保存到媒体库并将文件加载回我的apps目录.然后删除媒体文件.我不将它留在媒体目录中的原因是,当应用程序被删除时,图像将被删除.
这是新代码.首先是意图调用
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(manager) != null)
{
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION,"From your Camera");
Uri mImageUri = App.mApplication.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
mCurrentPhotoPath = mImageUri.getPath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(takePictureIntent, 1);
}
Run Code Online (Sandbox Code Playgroud)
然后是回调.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == -1)
{
if (data != null)
mCurrentPhotoPath = String.valueOf(data.getData());
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = App.mApplication.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, null, null, null);
if (cursor == null)
return;
// find the file in the media area
cursor.moveToLast();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
File source = new File(filePath);
cursor.close();
// create the destination file
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "slide_" + timeStamp;
File destination = new File(mStorageDirectory, imageFileName + ".jpg");
// copy the data
if (source.exists())
{
try
{
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
source.delete();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6108 次 |
最近记录: |