图片保存在Android上的SD卡上

时间:2012-06-18 19:11:52

标签: java android

从我的应用程序在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

2 个答案:

答案 0 :(得分: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;
  }

我希望这会对你有所帮助..     }

答案 1 :(得分:0)

您是否有权存储到SD卡?我相信您需要保存并保存为SD权限。