Java android上SD卡上保存的图片

Java android上SD卡上保存的图片,java,android,Java,Android,从我的应用程序将图片保存到SD卡时出现问题。 当我拍摄一张照片并将其保存在SD卡上,然后转到我的应用程序,拍摄一张新照片并将其保存在SD卡上时,会出现上一张预览照片,当在我的计算机上查看时,它会出现损坏 为什么会出现这个问题 public static void save(Bitmap bm, String path) { OutputStream outStream = null; try { outStream = new FileOutputStream(

从我的应用程序将图片保存到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

使用此方法存储并显示图像。此方法用于存储图像

             //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;
  }
查看图像

             //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;
  }
我希望这对你有帮助。。
}

您是否具有存储到SD卡的权限?我相信您需要保存和保存到sd权限。

您需要我用来保存图像的代码???是的。否则,任何人都不可能知道哪里出了问题。更具体地说,我们需要任何必要的代码来帮助您解决问题。仅仅说“我试图保存图片,但它已损坏”是远远不够的信息。你理解我的问题@vino吗????因为我的问题不在于如何保存图像并显示它。。我理解你的问题。尝试此方法,然后您可以在SD卡或pc中看到正确的图像,而不会出现任何问题。。