2013-03-02 78 views
1

當我使用以下代碼時,位圖將被保存並插入到相冊中「相機」相冊的末尾。將位圖保存到相冊

MediaStore.Images.Media.insertImage(getContentResolver(), bmp, null , null); 

如何將位圖保存到應用程序自己的相冊中?

+0

創建你的應用程序名稱的目錄,並保存在裏面,什麼問題? – Shiv 2013-03-02 07:28:12

回答

1
private boolean createFolder(String folderName) { 
    // TODO Auto-generated method stub 
    boolean success = true; 
    if(checkforMedia()) 
    { 
     String newFolder = "/OnTheG/"+folderName; 
     String extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
     File myNewFolder = new File(extStorageDirectory + newFolder); 
     success= myNewFolder.mkdirs(); 
    } 

    else 
    { 
     success=false; 
    } 
    return success; 


} 

use this function to create your folder in sdcard. 

Open the camera using this function to store your clicked image at your folder 


public void takePhoto1() { 
     if (!android.os.Environment.getExternalStorageState().equals(
       android.os.Environment.MEDIA_MOUNTED)) { 
      Toast.makeText(Add_View_Images_Activity.this, 
        "Please insert SDcard for capturing photo.", 
        Toast.LENGTH_SHORT).show(); 
     } else { 

      try { 

       photo1=new File(path+"/"+System.currentTimeMillis()+".jpg"); 
       Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
       cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo1)); 
       cameraIntent.putExtra("return-data", true); 

       startActivityForResult(cameraIntent, 4); 
      } catch (Exception e) { 
       Toast.makeText(Add_View_Images_Activity.this, ""+e, Toast.LENGTH_LONG).show(); 
      } 
     } 

    } 


and If you pick a image from the gallery obtain it's path and then move it to your folder using this function 


    private void moveTheImage(String path) { 
      // TODO Auto-generated method stub 
      // File sd = Environment.getExternalStorageDirectory(); 
      //  File data = Environment.getDataDirectory(); 

      String sourceImagePath= path; 
      System.out.println("Source path>>>>>>>>>"+path); 

      String destinationImagePath= fWrapper.path+getTheName(path); 

      File source= new File(sourceImagePath); 
      File destination= new File(destinationImagePath); 
      Log.d("before copying", ""); 
      if (source.exists()) { 
       try 
       { 
        FileChannel src = new FileInputStream(source).getChannel(); 
        FileChannel dst = new FileOutputStream(destination).getChannel(); 
        dst.transferFrom(src, 0, src.size()); 
        src.close(); 
        dst.close(); 
       } 

       catch (Exception e) { 
        // TODO: handle exception 
       } 
      } 
     } 
+0

當我打開畫廊的應用程序,我看到相冊,剪貼簿照片,即時上傳和其他一些相冊。保存照片後,我想將其放置在可從Gallery應用中看到的自定義相冊中。 – primary0 2013-03-02 07:51:12

+0

你需要點擊並存儲在你自己的相冊中的照片是你想要的,我會相應地編輯我的答案 – 2013-03-02 07:54:30

+0

是的,這就是我需要的。謝謝! – primary0 2013-03-02 07:58:19