2011-11-24 75 views
10

我有一個ImageView與共享意圖(這很好,帶來了所有的應用程序,我可以分享圖像),但是,我不能分享照片,因爲它沒有我的手機上的路徑。我如何着手將ImageView保存在手機上?以下是我的代碼。如何將ImageView保存爲圖像?

public void taptoshare(View v) 
{ 
    View content = findViewById(R.id.myimage); 
    content.setDrawingCacheEnabled(true); 
     Bitmap bitmap = content.getDrawingCache(); 
     File file = new File("/DCIM/Camera/image.jpg"); 
     try 
     { 
      file.createNewFile(); 
      FileOutputStream ostream = new FileOutputStream(file); 
      bitmap.compress(CompressFormat.JPEG, 100, ostream); 
      ostream.close(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 


    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg"); 
    shareIntent.setData(phototUri); 
    shareIntent.setType("image/*"); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri); 
    startActivity(Intent.createChooser(shareIntent, "Share Via")); 

} 
} 

UPDATE 好了,所以我想它了。現在我有一個新的問題,我將如何去將這個圖像保存到一個新的文件夾?

+0

應該單獨提出一個新問題,因爲您無法爲單個問題授予多個答案。但無論如何,File有一個mkdirs()方法可用於確保指定的文件夾存在。 – AlbeyAmakiir

+0

好的,謝謝@AlbeyAmakiir!留意未來的帖子! – dabious

回答

4

保存和加載時,首先需要獲取系統的根路徑。我就是這麼做的。

File root = Environment.getExternalStorageDirectory(); 
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg"); 
+0

我已經添加了,但它仍然無法正常工作..一些建議? – dabious

+0

它現在的作品!感謝一堆! – dabious

+2

@dabious可以分享工作解決方案嗎? – User404

1

我遇到了一些解決方案,它們沒有解決這個問題。

這是一個適合我的解決方案。有一個問題是你需要存儲在共享或非應用私人的位置(http://developer.android.com/guide/topics/data/data-storage.html#InternalCache

許多建議說,在Apps「私有」緩存位置存儲圖像,但這當然不是通過其他外部應用程序,包括accessable正在使用的通用共享文件意圖。當你嘗試這個,它會運行,但例如dropbox會告訴你該文件不再可用。

/*第1步 - 使用下面的文件保存功能在本地保存位圖文件。 */

localAbsoluteFilePath = saveImageLocally(bitmapImage); 

/*第2步 - 分享非私人文件絕對路徑共享文件意圖*/

if (localAbsoluteFilePath!=null && localAbsoluteFilePath!="") { 

    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    Uri phototUri = Uri.parse(localAbsoluteFilePath); 

    File file = new File(phototUri.getPath()); 

    Log.d("file path: " +file.getPath(), TAG); 

    if(file.exists()) { 
     // file create success 

    } else { 
     // file create fail 
    } 
    shareIntent.setData(phototUri); 
    shareIntent.setType("image/png"); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri); 
    activity.startActivityForResult(Intent.createChooser(shareIntent, "Share Via"), Navigator.REQUEST_SHARE_ACTION); 
} 

/*保存圖像功能*/

private String saveImageLocally(Bitmap _bitmap) { 

     File outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS); 
     File outputFile = null; 
     try { 
      outputFile = File.createTempFile("tmp", ".png", outputDir); 
     } catch (IOException e1) { 
      // handle exception 
     } 

     try { 
      FileOutputStream out = new FileOutputStream(outputFile); 
      _bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); 
      out.close(); 

     } catch (Exception e) { 
      // handle exception 
     } 

     return outputFile.getAbsolutePath(); 
    } 

/*步驟3 - 處理共享文件意圖結果。需要遠程臨時文件等。*/

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

      // deal with this with whatever constant you use. i have a navigator object to handle my navigation so it also holds all mys constants for intents 
     if (requestCode== Navigator.REQUEST_SHARE_ACTION) { 
      // delete temp file 
      File file = new File (localAbsoluteFilePath); 
      file.delete(); 

      Toaster toast = new Toaster(activity); 
      toast.popBurntToast("Successfully shared"); 
     } 


    } 

/* * UTILS/

public class Utils { 
    //... 
    public static File getAlbumStorageDir(String albumName) { 

     // Get the directory for the user's public pictures directory. 
     File file = 
      new File(Environment.getExternalStorageDirectory(), albumName); 
     if (!file.mkdirs()) { 
      Log.e(TAG, "Directory not created"); 
     } 
     return file; 
    } 
    //... 
} 

我希望可以幫助別人。

+0

我在步驟2(什麼是實用程序?)出錯:File outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS); –

+0

@DinIslamMilon,對此感到遺憾,自從此工作以來,它已經有一段時間了。我已經添加了我使用的custom utils靜態類。它獲取用戶圖片目錄 – wired00