Android“拍照” - 单击的图像被保存为损坏的图像
Posted
技术标签:
【中文标题】Android“拍照” - 单击的图像被保存为损坏的图像【英文标题】:Android "TAKE PHOTO" - Image which is clicked is getting saved as a corrupt image 【发布时间】:2015-07-30 18:41:24 【问题描述】:我有一个按钮,它会打开一个对话框,要求用户选择“拍照”或“从图库中选择”。
我遇到问题,当用户“拍照”时,图像被点击,出于验证目的,我在圆形图像视图中设置位图图像,但是当我转到指定的位置路径时图像,图像不存在或图像已损坏。
我还尝试在 android 中使用 AsyncHttpClient 将图像上传到服务器,但无法成功。
每次我收到 Java Socket TimeOut 异常。
下面是我的Camera Intent Activity
的代码public class AddAnUpdateActivity extends ActionBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
this.composeEditText = (EditText) findViewById(R.id.composeEditText);
setContentView(R.layout.add_update);
ProfilePictureImage = (CircularImageView) findViewById(R.id.ProfilePic);
insertVideo = (ImageButton) findViewById(R.id.insertVideoButton);
setBtnListenerOrDisable(insertVideo,mTakeVidOnClickListener, MediaStore.ACTION_VIDEO_CAPTURE);
insertImage = (ImageButton) findViewById(R.id.insertImageButton);
insertImage.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
selectImage();
);
private void setBtnListenerOrDisable(ImageButton btn,
Button.OnClickListener onClickListener,
String intentName)
if (isIntentAvailable(this, intentName))
btn.setOnClickListener(onClickListener);
else
btn.setClickable(false);
private boolean isIntentAvailable(Context context, String action)
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
private void selectImage()
final CharSequence[] options = "Take Photo", "Choose from Gallery","Cancel" ;
AlertDialog.Builder builder = new AlertDialog.Builder(AddAnUpdateActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(options,new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int item)
if(options[item].equals("Take Photo"))
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "Image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
else if (options[item].equals("Choose from Gallery"))
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
else if (options[item].equals("Cancel"))
dialog.dismiss();
);
builder.show();
@SuppressLint("Assert")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
if (requestCode == 1)
File f = new File(Environment.getExternalStorageDirectory().toString());
Log.d("PhotoImage","file path:"+f);
Log.d("PhotoImage","list of file path:"+ Arrays.toString(f.listFiles()));
for (File temp : f.listFiles())
if (temp.getName().equals("Image.jpg"))
Log.w("PhotoImage","enter in if block");
f = temp;
break;
try
Log.w("PhotoImage","enter in else block");
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),bitmapOptions);
ProfilePictureImage.setImageBitmap(bitmap);
if(bitmap!=null)
bitmap.recycle();
bitmap=null;
String path = android.os.Environment.getExternalStorageDirectory()+ File.separator+ "Pictures" + File.separator + "Screenshots";
Log.w("PhotoImage","path where the image is stored :"+path);
setFilePath(path);
f.delete();
OutputStream outFile;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
Log.w("PhotoImage","file value:"+String.valueOf(System.currentTimeMillis()) + ".jpg");
try
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
catch (Exception e)
e.printStackTrace();
catch (Exception e)
e.printStackTrace();
else if (requestCode == 2)
Uri selectedImage = data.getData();
String[] filePath = MediaStore.Images.Media.DATA ;
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
setFilePath(picturePath);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.d("PhotoImage path of image from gallery......******************.........", picturePath + "");
ProfilePictureImage.setImageBitmap(thumbnail);
else if(requestCode == 3)
handleCameraVideo(data) ;
private void handleCameraVideo(Intent data)
VideoUri = data.getData();
VideoView.setVideoURI(VideoUri);
//mImageBitmap = null;
private void startActivityFeedActivity()
Intent i = new Intent(getApplicationContext(), ActivityFeedActivity.class);
startActivity(i);
【问题讨论】:
为什么使用 ACTION_VIDEO_CAPTURE 进行图像捕捉?对于图像捕获,必须使用 ACTION_IMAGE_CAPTURE 和 EXTRA_OUTPUT 来定位图像。 我在早期版本的应用程序中进行了视频捕获!我需要评论该代码! 您发布了很多代码。如果是,请删除最多。只留下有问题的代码。并告诉代码应该做什么。同时删除上传代码。您最好将其作为单独的帖子发布。 您已经为私有 ImageButton insertImage 声明了变量;私人图像按钮插入视频;那么为什么要为视频调用“ImageButton.OnclickListener”,也许您必须编写 insertVideo.OnclickListener。 有没有给外部存储添加读写权限。 【参考方案1】:我简化了你的代码。保持全局文件路径引用
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "Image.jpg");
globalpath =f.getAbsolutePath(); //String make it global
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
//你的活动结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
if (requestCode == 1)
File myfile = new File(globalpath);
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(myfile.getAbsolutePath(),
bitmapOptions);
ProfilePictureImage.setImageBitmap(bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Pictures" + File.separator + "Screenshots";
OutputStream outFile;
File file = new File(path, String.valueOf(System
.currentTimeMillis()) + ".jpg");
try
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
myfile.delete();
catch (Exception e)
e.printStackTrace();
【讨论】:
【参考方案2】:根据您的 Android 版本和设备,相机 Intent 的实现方式会有所不同。查看https://github.com/ralfgehrer/AndroidCameraUtil。该代码在 100 多台设备上进行了测试。
【讨论】:
【参考方案3】:拍照后记得用这个:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));
扫描您画廊中的媒体文件。如果你不这样做,你的照片会在一段时间后出现。您可以在 onClick 中完成:
insertImage.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
selectImage();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));
);
【讨论】:
以上是关于Android“拍照” - 单击的图像被保存为损坏的图像的主要内容,如果未能解决你的问题,请参考以下文章
Excel图像损坏,没有被指定在windows上运行,或者包含错误怎么办?
当我尝试捕获图像(内置相机)并将其保存到文件时出现 NullPointerException [重复]