即使我决定不拍摄图像并按下后退按钮退出相机屏幕,也会在图库中生成并存储“空图像”?有没有办法在MainActivity.java
中消除这个连续的“空白图像”问题而不在我的库中的CameraPhoto
类中进行编辑?感谢
MainActivity.java
CameraPicture cameraPhoto = new CameraPicture(getApplicationContext());
Camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
startActivityForResult(cameraPhoto.takePhotoIntent(), CAMERA_REQUEST);
cameraPhoto.addToGallery();
}
catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK){
if(requestCode == CAMERA_REQUEST){
String photoPath = cameraPhoto.getPhotoPath();
selectedPhoto = photoPath;
Bitmap bitmap = null;
try {
bitmap = ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
ivImage.setImageBitmap(getRotatedBitmap(bitmap,90));
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(),"Something Wrong while loading the photo!", Toast.LENGTH_SHORT).show();
}
}
}
编辑:创建另一个名为CameraPicture的java类来托管更新的推荐代码,而不是从库CameraPhoto .jar文件中调用以防止冲突。
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CameraPicture {
private String photoPath;
private Context context;
public String getPhotoPath() {
return this.photoPath;
}
public CameraPicture(Context context) {
this.context = context;
}
public Intent takePhotoIntent() throws IOException {
Intent in = new Intent("android.media.action.IMAGE_CAPTURE");
if(in.resolveActivity(this.context.getPackageManager()) != null) {
File photoFile = this.createImageFileName();
if(photoFile != null) {
in.putExtra("output", Uri.fromFile(photoFile));
}
}
return in;
}
private File createImageFileName() throws IOException {
String timeStamp = (new SimpleDateFormat("yyyyMMdd_HHmmss")).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = new File ( storageDir + "/" + imageFileName + ".jpg");
this.photoPath = image.getAbsolutePath();
return image;
}
public void addToGallery() {
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
File f = new File(this.photoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.context.sendBroadcast(mediaScanIntent);
}
}
答案 0 :(得分:0)
cameraPhoto.addToGallery();
OMG。你在点击监听器中调用它。非常错误的地方。您应该只在onActivityResult()
中执行此操作。
拍照后才拍照。
说完我发现功能名称错了。您没有向Gallery添加任何内容,因为没有Gallery。只有一个图库应用程序。并且通常有更多的图库应用程序。您正在将文件添加到MediaStore。 Gallery和Fotos应用程序在MediaStore中查询图像。