Android,调试 MediaStore.ACTION_IMAGE_CAPTURE Intent
我试图通过以下方式用 MediaStore.ACTION_IMAGE_CAPTURE 意图拍照:
String fileName = "testphoto.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE)
.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
R.id.capture_image_request);
处理活动结果:
try {
uploadedPicture = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
然后我尝试显示刚刚拍摄的图像:
((ImageView) findViewById(R.id.profile_picture_thumbnail)).setImageBitmap(uploadedPicture);
但这不起作用。之后我尝试从该 Uri 打开一个输入流:
final InputStream is = getContentResolver().openInputStream(uri);
Log.("mydebug tag", is.available());
is.available() 返回 0。为什么该文件是空的???
当我尝试调试时,我发现像“content://media/external/images/media/68”这样的Uri已经被返回。 之后我进行了硬编码:
Uri uri = Uri.parse("content://media/external/images/media/68");
Bitmap pic;
try {
pic = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
((ImageView) findViewById(R.id.profile_picture_thumbnail)).setImageBitmap(pic);
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage());
}
这有效了。
所以我想知道为什么当我手动构建 Uri 时 - 它可以工作,但是当我收到刚刚拍摄的照片的 Uri 时 - 它不工作?我有一种感觉,当我访问刚刚拍摄的图片的 Uri 时,它还没有存储在文件系统中,这就是为什么它的大小等于 0 并且无法加载到 ImageView 中。但一段时间后(当我使用硬编码的 Uri 再次运行我的应用程序时)文件被存储并正常工作。有什么方法可以将该文件强制刷新到磁盘吗?
I am trying to take a picture with MediaStore.ACTION_IMAGE_CAPTURE intent in the following way:
String fileName = "testphoto.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE)
.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI),
R.id.capture_image_request);
Handling activity result:
try {
uploadedPicture = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
And after that I'm trying to show just taken image:
((ImageView) findViewById(R.id.profile_picture_thumbnail)).setImageBitmap(uploadedPicture);
And that doesn't work. After that I tried to open an input stream from that Uri:
final InputStream is = getContentResolver().openInputStream(uri);
Log.("mydebug tag", is.available());
is.available() returns 0. Why is that file empty???
When I tried to debug I found that Uri like "content://media/external/images/media/68" had been returned.
After that I hardcoded:
Uri uri = Uri.parse("content://media/external/images/media/68");
Bitmap pic;
try {
pic = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
((ImageView) findViewById(R.id.profile_picture_thumbnail)).setImageBitmap(pic);
} catch (FileNotFoundException e) {
Log.e(TAG, e.getMessage());
}
And that worked.
So I wonder why when I construct Uri manually - it works, but when I recieve a Uri of just taken picture - it doesn't? I have a feeling that when I access Uri of just taken picture, it isn't stored on a file system yet, that's why it's size is equal to 0 and can't be loaded into ImageView. But after a time (when I run my application again, with hardcoded Uri) file gets stored and works fine. Are there any ways to make a force flush of that file to a disk?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论