1

enter image description here如果我使用的ContentProvider在我的應用我得到錯誤,如‘不幸的是相機已停止’後Result_ok.This是我的代碼:不幸相機已停止」,而使用內容提供商

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
         i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI); 
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

如何解決這個?我不想保存的圖像在SD卡

這是我MyFileContentProvider類:

public class MyFileContentProvider extends ContentProvider { 

    public static final Uri CONTENT_URI = Uri.parse("content://com.example.user.studentadmission/"); 
    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(), "student.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(), "student.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"); 
    } 

} 
+0

發表您的logcat的消息。這將有助於理解問題。 – ajantha

+0

@Amshu我建議你去通過[這](https://github.com/google/cameraview)爲例,其良好的一個。 – Nisarg

+0

@ajantha我在logcat中沒有收到任何消息。 – Amshu

回答

2

很少有相機的動作只適用於Kitkat,使用動作條件:

Intent i = (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) 
         ? new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE) 
         : new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI); 
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

如果仍然發生崩潰,請嘗試避免使用ContentProvider進行測試。讓我知道。

+0

還是我得到同樣的錯誤error.This在5.1版本及版本6.0.1 – Amshu

+0

工作正常,如果我評論i.putExtra(MediaStore.EXTRA_OUTPUT,MyFileContentProvider.CONTENT_URI);沒有問題,但圖像質量會變得模糊。 – Amshu

+0

模糊圖像與內容提供商無關。儘量不要使用它。 – W4R10CK

0

給予准許的開放式攝像機。 爲打開的相機提供運行時權限。