2016-05-13 38 views
2

我正在使用ACTION_IMAGE_CAPTURE與預定目標Uri非常符合文檔中的建議。但是,當我的活動獲取後立即嘗試解碼圖像時,decodeStream()失敗。如果幾秒鐘後再次嘗試,它可以正常工作。我想這個文件是在後臺異步寫入的。我如何知道它何時可以使用?使用ACTION_IMAGE_CAPTURE拍攝的圖片 - 第一個decodeStream調用失敗,其他確定

這裏是我的代碼的關鍵部分:拍照

String filename = String.format("pic%d.jpg", new Date().getTime()); 
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename); 

try { 
    file.createNewFile(); 
} catch (IOException e) { 
    file = new File(context.getFilesDir(), filename); 
} 
targetUri = Uri.fromFile(photoFile); 

確定目標文件名

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, targetUri); 
fragment.startActivityForResult(takePictureIntent, RESULT_TAKE_PICTURE); 

onActivityResult()

if (resultCode == Activity.RESULT_OK) { 
    if (data != null) { 
     // Note that data.getData() is null here. 
     InputStream is = getContentResolver().openInputStream(targetUri); 
     if (is != null) { 
      Bitmap bm = BitmapFactory.decodeStream(is); 

decodeStream返回null。如果幾秒鐘後再次進行相同的呼叫,則會成功。有什麼文件可用時告訴我什麼?

UPDATE:繼greenapps'的建議,我在做decodeStream電話與inJustDecodeBounds率先拿到尺寸,看它是否是一個記憶的問題。發現這個第一個僅限界限的解碼傳遞失敗,但是現在實際的decodeStream調用立即成功!如果我再做兩次,他們都成功了!

因此,看起來第一個撥打decodeStream的電話總是失敗,其他所有電話都很好,即使它們之後立即發生(=在同一方法中)。所以這可能不是一個異步寫入的問題。但別的東西。但是什麼?

+0

targetURI中和URI? – greenapps

+0

你是如何設定targetUri的?從文件系統路徑?然後在原始路徑上使用decodeFile。 – greenapps

+0

'我想這個文件是在後臺異步寫入的。我認爲decodeStream由於內存不足而返回null。一段時間後,gc已經恢復得足夠了。嘗試只解碼邊界。 – greenapps

回答

0
public static void updateFile(File file ,Context context) { 
     MediaScannerConnection.scanFile(context, 
       new String[]{file.getAbsolutePath()}, null, 
       new MediaScannerConnection.OnScanCompletedListener() { 
        public void onScanCompleted(String path, Uri uri) { 
        } 
       } 
     ); 
    } 

我想你可以在openInputStream之前使用它來更新。

0

從片段您共享我thiiink的問題是,要設置圖片文件到文件變量文件

File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename); 

但你從文件photoFile這可能是空的設置圖像烏里

targetUri = Uri.fromFile(photoFile); 

所以基本上你需要用targetUri = Uri.fromFile(file);代替targetUri = Uri.fromFile(photoFile);

甚至更​​好data.getData()將返回圖片URI directlty這樣

InputStream is = null; 
     try { 
      is = getContentResolver().openInputStream(data.getData()); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
     if (is != null) { 
      Bitmap bm = BitmapFactory.decodeStream(is); 

      ((ImageView) findViewById(R.id.imageView1)).setImageBitmap(bm); 
     } 

你仍然需要解碼位圖,以避免您可以使用Glide使用圖像URI加載圖像OOM異常。

完整的類測試在Xpriaç

package com.example.test; 

    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.util.Date; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.os.Environment; 
    import android.provider.MediaStore; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.ImageView; 

    public class TestActivity extends Activity { 

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

      findViewById(R.id.take_picture).setOnClickListener(
        new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          dispatchTakePictureIntent(); 
         } 
        }); 
     } 

     static final int REQUEST_TAKE_PHOTO = 1; 

     private void dispatchTakePictureIntent() { 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      // Ensure that there's a camera activity to handle the intent 
      if (takePictureIntent.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 
       } 
       // Continue only if the File was successfully created 
       if (photoFile != null) { 
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
          Uri.fromFile(photoFile)); 
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
       } 
      } 
     } 

     protected static final int RESULT_TAKE_PICTURE = 100; 

     private File createImageFile() throws IOException { 
      // Create an image file name 
      String filename = String.format("pic%d.jpg", new Date().getTime()); 
      File file = new File(
        getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename); 

      try { 
       file.createNewFile(); 
      } catch (IOException e) { 
       file = new File(getFilesDir(), filename); 
      } 

      return file; 
     } 

     @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) { 

       InputStream is = null; 
       try { 
        is = getContentResolver().openInputStream(data.getData()); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
       if (is != null) { 
        Bitmap bm = BitmapFactory.decodeStream(is); 

        ((ImageView) findViewById(R.id.imageView1)).setImageBitmap(bm); 
       } 
      } 


} 
} 
2
if (requestCode == Utility.GALLERY_PICTURE) { 

     Uri selectedImageUri = null; 
     try { 
      selectedImageUri = data.getData(); 
      if (mImgProfilePic != null) { 
       // mImgProfilePic.setImageURI(selectedImageUri); 
       mImgProfilePic.setImageBitmap(decodeUri(getActivity(), 
         selectedImageUri, 60)); 
       // decodeUri 

      } 
     } catch (Exception e) { 

     } 
     // ////////////// 
     try { 
      // Bundle extras = data.getExtras(); 
      // // get the cropped bitmap 
      // Bitmap thePic = extras.getParcelable("data"); 
      // mImgProfilePic.setImageBitmap(thePic); 

      final Uri tempUri = selectedImageUri; 
      Log.d("check", "uri " + tempUri); 
      // http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=companylogo 
      upLoadServerUri = "http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=employee_profile_pic&image="; 
      upLoadServerUri = Utility.EMPLOYEE_PROFILE_PIC_URL 
        + "&employee_id=" + empId; 
      dialog = ProgressDialog.show(getActivity(), "", 
        "Uploading file...", true); 

      new Thread(new Runnable() { 
       public void run() { 
        getActivity().runOnUiThread(new Runnable() { 
         public void run() { 
          // messageText.setText("uploading started....."); 
         } 
        }); 
        uploadFilePath = getRealPathFromURI(tempUri); 
        uploadFile(uploadFilePath + ""); 
        // uploadFile(tempUri+""); 
       } 
      }).start(); 

     } catch (Exception e) { 

     } 

     // /////// 

    }