-1

从我的应用程序将图片保存到 sdcard 时出现问题。当我拍照并将其保存在 sdcard 上并转到我的应用程序并拍摄一个新的并将其保存在 sdcard 上时,会出现以前的预览图片,当在我的计算机上查看它时它似乎已损坏?

为什么会出现这个问题?

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 卡?我相信您需要保存并保存到 sd 权限。

于 2012-06-19T02:21:35.953 回答