-1

アプリから SD カードに画像を保存するときに問題が発生しました。写真を撮ってSDカードに保存し、アプリに移動して新しい写真を撮ってSDカードに保存すると、以前のプレビュー画像が表示され、コンピューターで表示すると破損しているように見えますか?

なぜこの問題?

public static void save(Bitmap bm, String path) {
    OutputStream outStream = null;

    try {
        outStream = new FileOutputStream(new File(path));
        bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();

        bm.recycle();
        System.gc();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

enter code here
4

2 に答える 2

2

この方法を使用して、画像を保存して表示します。これは、画像を保存するために使用されます。

             //create new directory to store image    
             File photo = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationContext().getPackageName()+"/files/Receipt"); 
             boolean success = false;
             if(!photo.exists())
             { 
                 success = photo.mkdirs(); 
             }
               //if exists save the image in specified path
             if(!success)
             {
                 dbimgguid = UUID.randomUUID();
                 imagename =dbimgguid.toString();
                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                 photo = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationContext().getPackageName()+"/files/Receipt", imagename+".png");
                 intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
                 imageurl = Uri.fromFile(photo); 
                 startActivityForResult(intent, CAMERA_RECEIPTREQUEST); 
             } 

画像を表示するには

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

     super.onActivityResult(requestCode, resultCode, data);  
     switch(requestCode)
     {
     case CAMERA_RECEIPTREQUEST:  
         if(resultCode== Activity.RESULT_OK)
         {
         //Toast.makeText(this, "Receipt Image Saved", Toast.LENGTH_SHORT).show();
         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inSampleSize = 8;
         ImageView jpgView = (ImageView)findViewById(R.id.imageView1);
         Bitmap receipt = BitmapFactory.decodeFile(photo.toString(),options); 
         jpgView.setImageBitmap(receipt);                 

         } 
         break;
  }

これがお役に立てば幸いです。}

于 2012-06-18T19:32:01.860 に答える
0

SDカードへの保存権限はありますか?save および save to sd 権限が必要だと思います。

于 2012-06-19T02:21:35.953 に答える