Android Camera意图创建两个文件
我正在制作一个程序,可以拍摄照片然后显示它的缩略图。 使用模拟器时一切顺利,丢弃按钮会删除照片。 但在真实设备上,相机意图将图像保存在 imageUri 变量中,而第二个变量的命名就像我刚刚打开相机并自行拍摄了一张照片一样。
private static final int CAMERA_PIC_REQUEST = 1337;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
//start camera
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION,"From your Camera");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
image = (ImageView) findViewById(R.id.ImageView01);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
//save the image buttons
Button save = (Button) findViewById(R.id.Button01);
Button close = (Button) findViewById(R.id.Button02);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
try{
thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
image.setImageBitmap(thumbnail);
}
catch(Exception e){
e.printStackTrace();
}
}
else{
finish();
}
}
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.Button01:
finish();
break;
case R.id.Button02:
dicard();
}
}
private void dicard(){
getContentResolver().delete(imageUri, null, null);
finish();
}
I am making a program that takes a picture and then shows it's thumbnail.
When using the emulator all goes well and the discard button deletes the photo.
But on a real device the camera intent saves the image at the imageUri variable and a second one that is named like if I had just opened up the camera and took a picture by itself.
private static final int CAMERA_PIC_REQUEST = 1337;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
//start camera
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION,"From your Camera");
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
image = (ImageView) findViewById(R.id.ImageView01);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
//save the image buttons
Button save = (Button) findViewById(R.id.Button01);
Button close = (Button) findViewById(R.id.Button02);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
try{
thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
image.setImageBitmap(thumbnail);
}
catch(Exception e){
e.printStackTrace();
}
}
else{
finish();
}
}
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.Button01:
finish();
break;
case R.id.Button02:
dicard();
}
}
private void dicard(){
getContentResolver().delete(imageUri, null, null);
finish();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
某些 Android 手机将原始照片存储在图库中,并仅将缩略图存储在您所在的位置。您对原始请求做了什么并不重要。我有两款不同的 HTC 手机可以做到这一点,还有许多其他品牌没有这样做。
我用另一种方式解决了这个问题。我对图库中的每个项目进行了查询,并将 BucketID 加载到一个数组中。当我的应用程序启动相机应用程序时,我会执行此操作。当相机应用程序返回时,我进行相同的查询(最近添加了项目以节省时间)。我将其与原始列表进行比较并找到新的 BucketID。接下来,我将此图像的大小与我明确设置为输出的文件进行比较。如果它更大,我会复制它,替换我拥有的。然后我删除该文件并将其从图库中删除。
痛苦在你知道的!
[编辑] 当我发现一部手机没有保留唯一的存储桶 ID 时,我不得不再次更改此设置...有关更多信息,请参阅此答案后面的链接中的我的帖子。
Some Android phones store the original photo in the gallery, and a thumbnail only in your location. It doesn't matter what you did with the original request. I have two different HTC phones doing it, and a slew of other brands not doing it.
I solved this another way. I ran a query of every item in the gallery and loaded the BucketIDs to an array. I do this when my app starts the camera app. When the camera app returns, I make the same query (with items recently added to save time). I compare this to my original list and find the new BucketID. Next, I compare the size of this image with the file I explicitly set as the output. If it's bigger, I copy it, replacing what I had. Then I delete the file and remove it from the gallery.
Pain in the you-know-what!
[EDIT] I had to change this around again when I discovered a phone that didn't keep unique bucket IDs... See my post in the link following this answer for more.