2017-11-08 5 views
0

私はカメラを起動して起動し、画像を自分の電話機に保存していますが、設定したImageViewに表示し、その後にアプリを起動するたびにもう一度表示します。私がこれを達成するために必要なことをどのように広げることができるかに関する提案はありますか?最近保存した画像をImageViewに送信するには?

private void takePic() { 
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    String pictureName = "Avatar.jpg"; 
    File imageFile = new File(pictureDirectory, pictureName); 
    Uri pictureUri = Uri.fromFile(imageFile); 
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri); 
    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
} 

そして、私は単にtakePic()に呼び出すボタンを持って次のように

私のカメラのコードがあります。私は単に私のImageViewのサムネイルを表示するために、これを使用しますが、私はそれを削除しなければならなかった画像を保存するためのコードを変更したとき

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
     Bitmap photo = (Bitmap) data.getExtras().get("data"); 
     AvatarMe.setImageBitmap(photo); 
    } 
} 

、そうでない場合はアプリがクラッシュしました:私はこのように見えた前onActivityResult()を持っていました。 Androidは私に両方をさせてくれないようですので、私はそれをどうやってできるのか理解するために何か助けが必要です。

基本的に私はImageViewで画像を表示して保存しておき、画像を撮影した後にアプリが強制終了された場合は保存した画像でImageViewを塗りつぶすことができます。

答えて

0

picassoライブラリで行うことができます。

+0

これは周り 'ビットマップ= MediaStore.Images.Media.getBitmap(これをキャッチ/トライをラップした後、あなたが –

+0

はい、私の評判はコメント人のためにあまりにも50にする必要がある:) –

+1

OK男をコメントする必要があります.getContentResolver()、pictureUri); ' とキャッチのトーストが、トーストトリガーとして何かうまくいかないようです。あなたが言ったように、それを 'Uri pictureUri;'と 'pictureUri = uri 'として定義することによって、Uriをグローバル変数として入れてみました。fromFile(imageFile); 'メソッド内にあります。 –

0

onActivityResult()のデータは意図していますが、内部ストレージに保存してイメージビューに表示する必要があります。

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 
{ 
if (resultCode == Activity.RESULT_OK) 
{ 
    Bitmap bmp = (Bitmap)data.getExtras().get("data"); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    byte[] byteArray = stream.toByteArray(); 

    //saving image into internal storage 

    File myfile = new File(Environment.getExternalStorageDirectory(),"yourfilename.jpg"); 

    FileOutputStream fo; 
    try { 
     myfile.createNewFile(); 
     fo = new FileOutputStream(myfile); 
     fo.write(byteArray); 
     fo.flush(); 
     fo.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    // convert byte array to Bitmap 

    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, 
      byteArray.length); 

    imageView.setImageBitmap(bitmap); 

} 
} 
} 

ハッピーコーディング!

0

私はあなたがpicasso libraryを使用するかは、私はそれを使用する私のアプローチは、thaの画像と、このImageCompressionクラスonActivityResultコールで、その後 `

private Context context; 
float maxHeight; 
float maxWidth; 
boolean wantSave; 

public ImageCompression(Context context) { 
    this.context = context; 
} 

public ImageCompression(Context context, float maxHeight, float maxWidth, boolean wantSave) { 
    this.context = context; 
    this.maxHeight = maxHeight; 
    this.maxWidth = maxWidth; 
    this.wantSave = wantSave; 
} 

@Override 
protected Bitmap doInBackground(String... strings) { 
    if (strings.length == 0 || strings[0] == null) 
     return null; 

    return compressImage(strings[0]); 
} 

protected void onPostExecute(Bitmap imagePath) { 
    // imagePath is path of new compressed image. 
} 


public Bitmap compressImage(String imagePath) { 
    // to check if image is exist or not 
    File checkFile = new File(imagePath); 
    if (!checkFile.exists()) { 
     return null; 
    } 

    Bitmap scaledBitmap = null; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    Bitmap bmp = BitmapFactory.decodeFile(imagePath, options); 

    int actualHeight = options.outHeight; 
    int actualWidth = options.outWidth; 

    float imgRatio = (float) actualWidth/(float) actualHeight; 
    float maxRatio = maxWidth/maxHeight; 

    if (actualHeight > maxHeight || actualWidth > maxWidth) { 
     if (imgRatio < maxRatio) { 
      imgRatio = maxHeight/actualHeight; 
      actualWidth = (int) (imgRatio * actualWidth); 
      actualHeight = (int) maxHeight; 
     } else if (imgRatio > maxRatio) { 
      imgRatio = maxWidth/actualWidth; 
      actualHeight = (int) (imgRatio * actualHeight); 
      actualWidth = (int) maxWidth; 
     } else { 
      actualHeight = (int) maxHeight; 
      actualWidth = (int) maxWidth; 

     } 
    } 

    options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight); 
    options.inJustDecodeBounds = false; 
    options.inDither = false; 
    options.inPurgeable = true; 
    options.inInputShareable = true; 
    options.inTempStorage = new byte[16 * 1024]; 

    try { 
     bmp = BitmapFactory.decodeFile(imagePath, options); 
     scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.RGB_565); 

     float ratioX = actualWidth/(float) options.outWidth; 
     float ratioY = actualHeight/(float) options.outHeight; 
     float middleX = actualWidth/2.0f; 
     float middleY = actualHeight/2.0f; 

     Matrix scaleMatrix = new Matrix(); 
     scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); 

     Canvas canvas = new Canvas(scaledBitmap); 
     canvas.setMatrix(scaleMatrix); 

     if (bmp != null) { 
      canvas.drawBitmap(bmp, middleX - bmp.getWidth()/2, middleY - bmp.getHeight()/2, new Paint(Paint.FILTER_BITMAP_FLAG)); 
      bmp.recycle(); 
     } 

     ExifInterface exif; 
     try { 
      exif = new ExifInterface(imagePath); 
      int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); 
      Matrix matrix = new Matrix(); 
      if (orientation == 6) { 
       matrix.postRotate(90); 
      } else if (orientation == 3) { 
       matrix.postRotate(180); 
      } else if (orientation == 8) { 
       matrix.postRotate(270); 
      } 
      scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     // these lines from 144 to 157 for save the new photo 
     if (wantSave) { 
      FileOutputStream out = null; 
      String filepath = imagePath; 
      try { 
       out = new FileOutputStream(filepath); 

       //write the compressed bitmap at the destination specified by filename. 
       scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); 

      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
     } 

     return scaledBitmap; 

    } catch (OutOfMemoryError exception) { 
     exception.printStackTrace(); 
    } 
    return null; 
} 

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     final int heightRatio = Math.round((float) height/(float) reqHeight); 
     final int widthRatio = Math.round((float) width/(float) reqWidth); 
     inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 
    final float totalPixels = width * height; 
    final float totalReqPixelsCap = reqWidth * reqHeight * 2; 

    while (totalPixels/(inSampleSize * inSampleSize) > totalReqPixelsCap) { 
     inSampleSize++; 
    } 
    return inSampleSize; 
} 

}

ImageCompression imageCompression = new ImageCompression(context, imageHeight, imageHeight, false) { 
     @Override 
     protected void onPostExecute(Bitmap bitmab) { 
      super.onPostExecute(bitmab); 
       try { 
        if (imagePath != null) { 
         mCropImageView.setImageBitmap(bitmab); 
        } 
       } catch (OutOfMemoryError error) { 
        Toast.makeText(CropImageActivity.this, "OutOfMemory, no space", Toast.LENGTH_SHORT).show(); 
       } 
     } 
    }; 
    imageCompression.execute(imagePath); 

を圧縮することができますこれが助けてくれることを願って

0

Tこの場合、カメラの意図からの "データ"はnullです。これはanswerです。

この問題を解決するには、カメラの意図をグローバルに渡す必要があります。

//make this a global variable 
Uri pictureUri = Uri.fromFile(imageFile); 

そして、あなたはまた、デシベルまたは共有プリファレンスを使用してpictureUriを保存し、後でそれを再利用することができ、この

void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) 
     { 
      Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), pictureUri); 
      AvatarMe.setImageBitmap(bitmap); 
     } 
    } 

のようなビットマップにアクセスすることができます。

+0

正しい – PQuix

+0

キャッチブロックのエラーを印刷し、私に知らせてください。あなたを助けることができるかもしれません。 – amarok

+0

これはあなたの後のことですか? https://pastebin.com/5bKHYx6y – PQuix

関連する問題