我正在尝试创建一个将采用图像和显示的Android应用程序 它是用户的进一步行动。
这是相机活动。当用户按下上一个活动中的浮动操作按钮时,它将启动。 Camera意图在onCreate本身内触发。
相机动作完美无缺,拍照很好。但它既没有保存在图库中,也没有显示在下面定义的ImageView中。
我正在尝试在ImageView中显示捕获的图像,但相机活动只是移回菜单活动而不显示任何内容。我该如何解决这个问题?
public class Camera extends AppCompatActivity {
public static final String TAG = Camera.class.getSimpleName();
static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView = (ImageView)findViewById(R.id.mImageView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_layout);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.i(TAG, "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
Log.v("This is totally working", "Yeah!");
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
mImageView.setImageBitmap(mImageBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这是布局的XML代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/mImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="7" />
</FrameLayout>
答案 0 :(得分:0)
在AndroidManifest.xml中添加WRITE_EXTERNAL_STORAGE
权限
答案 1 :(得分:0)
您可以尝试使用Magical Take Photo库。
<强> 1。用gradle编译
compile 'com.frosquivel:magicaltakephoto:1.0'
<强> 2。您需要在manifest.xml中使用此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
第3。像这样的类
//&#34;这&#34;是当前的活动参数
MagicalTakePhoto magicalTakePhoto = new MagicalTakePhoto(this,ANY_INTEGER_0_TO_4000_FOR_QUALITY);
<强> 4。如果你需要拍照,请使用方法
magicalTakePhoto.takePhoto("my_photo_name");
<强> 5。如果您需要在设备中选择图片,请尝试使用以下方法:
magicalTakePhoto.selectedPicture("my_header_name");
<强> 6。您需要覆盖活动或片段的onActivityResult方法,如下所示:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
magicalTakePhoto.resultPhoto(requestCode, resultCode, data);
//example to get photo
//imageView.setImageBitmap(magicalTakePhoto.getMyPhoto());
}
注意:只有使用此库,您才可以在设备中选择图片,这将使用最小的API 15。