2017-01-27 30 views
0

目前我正在創建允許用戶拍照並將其顯示給ImageView的應用程序。它的作用就像Android 5.1.1上的魅力一樣。 但是在Kitkat 4.4.2三星Galaxy Tab 3和KitKat 4.4.4小米紅米2上,拍照後相機強行關閉。在KitKat捕獲後關閉Android Camera功能

我不知道這是否有用,但我意識到相機可能會強制關閉,因爲在捕獲後,在這兩個KitKat設備上,用戶將被提示接受捕獲的圖片或不,然後如果接受,它會回到我目前的活動。

由於我的索尼5.1.1版本,用戶不會被提示輸入捕獲的圖片,它會直接返回到我當前的活動。

這裏我包含了相應的代碼。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_job_order_line_form); 

    bAddPhoto = (TextView) findViewById(R.id.bAddPhoto); 
    bSaveJOLine = (TextView) findViewById(R.id.bSaveJOLine); 

    editDescription = (EditText) findViewById(R.id.editDescription); 
    editQty = (EditText) findViewById(R.id.editQty); 
    editPrice = (EditText) findViewById(R.id.editPrice); 
    ivImage = (ImageView) findViewById(R.id.imageTaken); 

    Intent intent = getIntent(); 
    jobId = intent.getIntExtra("jobId", 0); 
    docNo = intent.getStringExtra("docNo"); 

    bAddPhoto.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (intentCamera.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 
        Toast.makeText(OJobOrderLineFormActivity.this, "Create file failed!", 
          Toast.LENGTH_SHORT).show(); 
       } 
       // Continue only if the File was successfully created 
       if (photoFile != null) { 
        Uri photoURI = FileProvider.getUriForFile(OJobOrderLineFormActivity.this, 
          "com.opentoko.opentokolaundry.fileprovider", 
          photoFile); 
        System.out.println(photoURI); 
        intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
        startActivityForResult(intentCamera, 360); 
       } 
      } 
     } 
    }); 

    bSaveJOLine.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Snackbar.make(v, "Item has been saved!", Snackbar.LENGTH_LONG) 
        .setAction("OK", null).show(); 
     } 
    }); 

} 

創建臨時文件功能:

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date()); 
    String imageFileName = docNo + "_" + timeStamp + "_"; 
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 
    System.out.println(storageDir); 
    System.out.println(image); 

    // Save a file: path for use with ACTION_VIEW intents 
    currentPhotoPath = image.getAbsolutePath(); 
    System.out.println(currentPhotoPath); 

    return image; 
} 

onActivityResult:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
     case 360: 
      if(resultCode == RESULT_OK) { 
       int targetW = ivImage.getWidth(); 
       int targetH = ivImage.getHeight(); 

       // Get the dimensions of the bitmap 
       BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
       bmOptions.inJustDecodeBounds = true; 
       BitmapFactory.decodeFile(currentPhotoPath, bmOptions); 
       int photoW = bmOptions.outWidth; 
       int photoH = bmOptions.outHeight; 

       // Determine how much to scale down the image 
       int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

       // Decode the image file into a Bitmap sized to fill the View 
       bmOptions.inJustDecodeBounds = false; 
       bmOptions.inSampleSize = scaleFactor; 
       bmOptions.inPurgeable = true; 

       Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions); 
       ivImage.setImageBitmap(bitmap); 
      } 
    } 
} 

我真的不能想出什麼,因爲在logcat中造成這種存在似乎沒有錯誤可言,我構建仍像往常一樣運行。 這裏是進入當前活動拍攝照片後,我的logcat:

D/TextLayoutCache: Enable myanmar Zawgyi converter 
D/TextLayoutCache: Enable myanmar Zawgyi converter 
D/TextLayoutCache: Enable myanmar Zawgyi converter 
D/TextLayoutCache: Enable myanmar Zawgyi converter 
I/System.out: /storage/emulated/0/Android/data/com.opentoko.opentokolaundry/files/Pictures 
I/System.out: /storage/emulated/0/Android/data/com.opentoko.opentokolaundry/files/Pictures/A-00003_20170127_1758_-1345208956.jpg 
I/System.out: /storage/emulated/0/Android/data/com.opentoko.opentokolaundry/files/Pictures/A-00003_20170127_1758_-1345208956.jpg 
I/System.out: content://com.opentoko.opentokolaundry.fileprovider/my_images/A-00003_20170127_1758_-1345208956.jpg 
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection 

這logcat的已經在這個確切的點,當它啓動開始打開攝像頭。捕獲後,沒有添加到logcat。

這是我的Android-的Manifest.xml的一部分:

<uses-feature android:name="android.hardware.camera" android:required="true" /> 

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <provider 
     android:name="android.support.v4.content.FileProvider" 
     android:authorities="com.opentoko.opentokolaundry.fileprovider" 
     android:exported="false" 
     android:grantUriPermissions="true"> 
     <meta-data 
      android:name="android.support.FILE_PROVIDER_PATHS" 
      android:resource="@xml/file_paths"> 
     </meta-data> 
    </provider> 

是否有人在這裏有或有同樣的問題嗎?任何解決方案任何幫助將不勝感激。

回答

0

我已經找到了針對KitKat的意向照相機力的解決方案,通過試驗和錯誤。 似乎我根本不需要FileProvider。

我在createImageFile()改變storageDir這樣:

File storageDir = getExternalFilesDir("Pictures"); 

而且photoURI這樣:

Uri photoURI = Uri.fromFile(photoFile); 

現在我有權利全屏圖像顯示在奇巧了。

我從清單中刪除提供程序。現在我在Android/data/my.package.name/files/Pictures /文件夾中獲取了捕獲的圖像文件。