2016-09-08 2 views
1

私のアプリでは、画像ファイル(カメラ付き携帯電話から撮影した画像)を保存しますが、サムネイルをすぐにImageviewに表示するとエラーになることがあります。どうして?保存プロセスが遅すぎますか?イメージファイルを保存するために代替手段を使用できますか?画像ファイルを保存するのが遅すぎるImageviewにすぐに表示するには

ここで私は私のカメラを呼び出します。ここでは

 camera.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      if (intent.resolveActivity(getActivity().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 
        Log.v("Crea file Immagine", "IOException"); 
       } 
       // Continue only if the File was successfully created 
       if (photoFile != null) { 
        Log.v("photofile",String.valueOf(photoFile)); 
        Uri mSelectedImageUri= Uri.fromFile(photoFile); 
        Log.v("mSelectedImageUri", String.valueOf(mSelectedImageUri)); 
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mSelectedImageUri); 
        startActivityForResult(intent, CAMERA_REQUEST); 
       } 
      } 
     } 
    }); 

私は私のイメージファイルを保存します。私は、このエラーに

java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory) 
libcore.io.IoBridge.open(IoBridge.java:409) 
java.io.FileInputStream.<init>(FileInputStream.java:78) 
java.io.FileInputStream.<init>(FileInputStream.java:105) 
android.content.ContentResolver.openInputStream(ContentResolver.java:630) 

com.example.android.swipetabs.FragmentTab2.decodeSampledBitmapFromUri(FragmentTab2.java: 

P.S.を取得

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    sharedPreference.save(context,"nomeFile",timeStamp); 

    File storageDir = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_DCIM) + File.separator + "BottiglieDiVino"); 
    boolean success; 
    if (!storageDir.exists()) { 
     success = storageDir.mkdir(); 
     if (success) { 
      // Do something on success 
      Log.v("Crea /BottiglieDiVino", "Ho Creato nuova directory"); 
     } else { 
      // Do something else on failure 
      Log.v("Crea /BottiglieDiVino", "ERRORE nel creare nuova directory"); 
     } 
    } 

    File image = new File(storageDir, timeStamp + ".jpg"); 
    // Save a file: path for use with ACTION_VIEW intents 
    percorso = image.getAbsolutePath(); 
    sharedPreference.save(context,"percorso", percorso); 
    Log.v("percorso", percorso); 
    return image; 
} 

、ここを最後の行に:)はエラーです。

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) { 

     percorso = sharedPreference.getValue(context, "percorso"); 

     sharedPreference.save(context,"check23", "ok"); 
     Log.v("percorso", percorso); 
     foto.setImageBitmap(display(percorso)); 

    } 
} 

    public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, int reqHeight) { 

    try { 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 

     BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, options); 

私を助けてください。

答えて

0

注:marashmallowまたは上にこれらのアクセス許可の実行時間を与え、あなたのコードを入れて許可があなたの他のデバイスに付与されたときには、

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

mainfestでランタイム

細かい作業です

public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 10; 

camera.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     permissions= new String[]{ 
        Manifest.permission.CAMERA, 
        Manifest.permission.WRITE_EXTERNAL_STORAGE}; 


      if (checkPermissions()) 
      { 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       if (intent.resolveActivity(demo.this.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 
         Log.v("Crea file Immagine", "IOException"); 
        } 
        // Continue only if the File was successfully created 
        if (photoFile != null) { 
         Log.v("photofile",String.valueOf(photoFile)); 
         Uri mSelectedImageUri= Uri.fromFile(photoFile); 
         Log.v("mSelectedImageUri", String.valueOf(mSelectedImageUri)); 
         intent.putExtra(MediaStore.EXTRA_OUTPUT, mSelectedImageUri); 
         startActivityForResult(intent, CAMERA_REQUEST); 
        } 
       } 



      } 


     } 

    }); 

private boolean checkPermissions() { 
    int result; 
    List<String> listPermissionsNeeded = new ArrayList<>(); 
    for (String p:permissions) { 
     result = ContextCompat.checkSelfPermission(this,p); 
     if (result != PackageManager.PERMISSION_GRANTED) { 
      listPermissionsNeeded.add(p); 
     } 
    } 
    if (!listPermissionsNeeded.isEmpty()) { 
     ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS); 
     return false; 
    } 
    return true; 
}  
関連する問題