Android相机片段

时间:2015-03-08 13:35:24

标签: android android-fragments android-camera

我有一个简单的片段,允许用户拍照,将其保存到图库并在imageView中输出。当我把它作为一个活动运行时它工作得很好但是当我把它转换成一个片段然后图像没有出现在图像视图中,任何想法我做错了什么?

Home.java

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");

}

}

1 个答案:

答案 0 :(得分:0)

我解决了,我没有注意到,在contentProvider中使用了错误的包名称的愚蠢错误

public static final Uri CONTENT_URI = Uri.parse(&#34; content://com.example.camerademo/");