Android 安卓相机碎片
我有一个简单的片段,允许用户拍摄一张照片,保存到他们的图库中,并在imageView中输出。当我只是将其作为一个活动运行时,它工作正常,但当我将其转换为一个片段时,图像就不会出现在图像视图中,知道我做错了什么吗 Home.javaAndroid 安卓相机碎片,android,android-fragments,android-camera,Android,Android Fragments,Android Camera,我有一个简单的片段,允许用户拍摄一张照片,保存到他们的图库中,并在imageView中输出。当我只是将其作为一个活动运行时,它工作正常,但当我将其转换为一个片段时,图像就不会出现在图像视图中,知道我做错了什么吗 Home.java import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.Bitma
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
public class Home extends Fragment implements View.OnClickListener {
/** Called when the activity is first created. */
private final int CAMERA_RESULT = 1;
private final String Tag = getClass().getName();
Button button1;
ImageView imageView1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_home, container, false);
super.onCreate(savedInstanceState);
button1 = (Button)rootView.findViewById(R.id.button1);
imageView1 = (ImageView)rootView.findViewById(R.id.imageView1);
button1.setOnClickListener(this);
return rootView;
}
public void onClick(View v) {
PackageManager pm = getActivity().getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
startActivityForResult(i, CAMERA_RESULT);
} else {
Toast.makeText(getActivity().getBaseContext(), "Camera is not available", Toast.LENGTH_LONG).show();
} }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(Tag, "Receive the camera result");
if (resultCode == getActivity().RESULT_OK && requestCode == CAMERA_RESULT) {
File out = new File(getActivity().getFilesDir(), "newImage.jpg");
Toast.makeText(getActivity().getBaseContext(),
"Image captured and stored successfully", Toast.LENGTH_LONG)
.show();
if(!out.exists()) {
Toast.makeText(getActivity().getBaseContext(),
"Error while capturing image", Toast.LENGTH_LONG)
.show();
return;
}
Bitmap mBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());
imageView1.setImageBitmap(mBitmap);
}
}
@Override
public void onDestroy() {
super.onDestroy();
imageView1 = null;
}
}
MyFileContentProvider类
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
public class MyFileContentProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://com.example.camerademo/");
private static final HashMap<String, String> MIME_TYPES = new HashMap<String, String>();
static {
MIME_TYPES.put(".jpg", "image/jpeg");
MIME_TYPES.put(".jpeg", "image/jpeg");
}
@Override
public boolean onCreate() {
try {
File mFile = new File(getContext().getFilesDir(), "newImage.jpg");
if(!mFile.exists()) {
mFile.createNewFile();
}
getContext().getContentResolver().notifyChange(CONTENT_URI, null);
return (true);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
public String getType(Uri uri) {
String path = uri.toString();
for (String extension : MIME_TYPES.keySet()) {
if (path.endsWith(extension)) {
return (MIME_TYPES.get(extension));
}
}
return (null);
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
File f = new File(getContext().getFilesDir(), "newImage.jpg");
if (f.exists()) {
return (ParcelFileDescriptor.open(f,
ParcelFileDescriptor.MODE_READ_WRITE));
}
throw new FileNotFoundException(uri.getPath());
}
@Override
public Cursor query(Uri url, String[] projection, String selection,
String[] selectionArgs, String sort) {
throw new RuntimeException("Operation not supported");
}
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
throw new RuntimeException("Operation not supported");
}
@Override
public int update(Uri uri, ContentValues values, String where,
String[] whereArgs) {
throw new RuntimeException("Operation not supported");
}
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
throw new RuntimeException("Operation not supported");
}
}
导入android.content.ContentProvider;
导入android.content.ContentValues;
导入android.database.Cursor;
导入android.net.Uri;
导入android.os.ParcelFileDescriptor;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.HashMap;
公共类MyFileContentProvider扩展了ContentProvider{
公共静态最终Uri内容\u Uri=Uri.parse(“content://com.example.camerademo/");
私有静态最终HashMap MIME_TYPES=new HashMap();
静止的{
MIME_类型.put(“.jpg”,“image/jpeg”);
MIME_类型.put(“.jpeg”,“image/jpeg”);
}
@凌驾
公共布尔onCreate(){
试一试{
File mFile=新文件(getContext().getFilesDir(),“newImage.jpg”);
如果(!mFile.exists()){
mFile.createNewFile();
}
getContext().getContentResolver().notifyChange(内容URI,null);
返回(真);
}捕获(例外e){
e、 printStackTrace();
返回false;
}
}
@凌驾
公共字符串getType(Uri){
字符串路径=uri.toString();
for(字符串扩展名:MIME_TYPES.keySet()){
if(路径endsWith(扩展)){
返回(MIME_TYPES.get(扩展));
}
}
返回(空);
}
@凌驾
公共ParcelFileDescriptor openFile(Uri,字符串模式)
抛出FileNotFoundException{
文件f=新文件(getContext().getFilesDir(),“newImage.jpg”);
如果(f.exists()){
返回(ParcelFileDescriptor.open)(f,
ParcelFileDescriptor.MODE_READ_WRITE));
}
抛出新的FileNotFoundException(uri.getPath());
}
@凌驾
公共游标查询(Uri url、字符串[]投影、字符串选择、,
字符串[]selectionArgs,字符串排序){
抛出新的RuntimeException(“不支持操作”);
}
@凌驾
公共Uri插入(Uri、ContentValues和initialValues){
抛出新的RuntimeException(“不支持操作”);
}
@凌驾
公共int更新(Uri、ContentValues、字符串,其中,
字符串[](RGS){
抛出新的RuntimeException(“不支持操作”);
}
@凌驾
public int delete(Uri、字符串where、字符串[]wherergs){
抛出新的RuntimeException(“不支持操作”);
}
}
我解决了这个问题,我没有注意到的愚蠢错误是在contentProvider中使用了错误的包名
公共静态最终Uri内容\u Uri=Uri.parse(“content://com.example.camerademo/");