2011-11-24 65 views
10

Tôi có một ImageView với mục đích chia sẻ (hoạt động tốt, mang đến tất cả các ứng dụng tôi có thể chia sẻ hình ảnh), tuy nhiên, tôi không thể chia sẻ ảnh vì nó không có đường dẫn trên điện thoại của tôi. Tôi làm cách nào để lưu ImageView trên điện thoại của mình? Dưới đây là mã của tôi.Làm cách nào để lưu ImageView dưới dạng hình ảnh?

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")); 

} 
} 

CẬP NHẬT Ok, vì vậy tôi figured it out. Bây giờ tôi có một câu hỏi mới, làm thế nào tôi sẽ đi về việc lưu hình ảnh này vào một thư mục mới?

+0

Câu hỏi mới nên được thực hiện riêng biệt vì bạn không thể trao nhiều câu trả lời cho một câu hỏi. Nhưng dù sao, File có một phương thức mkdirs() có thể được sử dụng để đảm bảo thư mục được chỉ định tồn tại. – AlbeyAmakiir

+0

oh ok cảm ơn @AlbeyAmakiir! Lưu ý cho các bài viết trong tương lai! – dabious

Trả lời

4

Khi lưu và tải, trước tiên bạn cần có đường dẫn gốc của hệ thống. Đây là cách tôi làm điều đó.

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

Tôi đã thêm điều đó vào nhưng nó vẫn không hoạt động ... mọi đề xuất? – dabious

+0

nó hoạt động ngay bây giờ! Cảm ơn nhiều!! – dabious

+2

@dabious bạn có thể chia sẻ giải pháp làm việc không. – User404

1

Tôi đã gặp một vài giải pháp không giải quyết được vấn đề này.

Đây là giải pháp phù hợp với tôi. Một lưu ý là bạn cần phải lưu trữ hình ảnh ở một vị trí riêng tư hoặc không phải ứng dụng riêng tư (http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)

Nhiều đề xuất lưu trữ ở vị trí bộ nhớ cache riêng tư, nhưng điều này không thể truy cập được thông qua các ứng dụng bên ngoài khác, bao gồm Mục đích chung của Tệp Chia sẻ đang được sử dụng. Khi bạn thử điều này, nó sẽ chạy nhưng ví dụ dropbox sẽ cho bạn biết các tập tin không còn có sẵn.

/* BƯỚC 1 - Lưu tệp bitmap cục bộ bằng cách sử dụng chức năng lưu tệp bên dưới. */

localAbsoluteFilePath = saveImageLocally(bitmapImage); 

/* Bước 2 - Chia sẻ đường dẫn tập tin tuyệt đối tin không đến thư mục chia sẻ ý định */

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); 
} 

/* SAVE IMAGE CHỨC NĂNG */

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(); 
    } 

/* BƯỚC 3 - Xử lý Share File Intent result. Cần tập tin từ xa tạm thời vv */

@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; 
    } 
    //... 
} 

Tôi hy vọng rằng sẽ giúp một ai đó.

+0

Tôi gặp lỗi ở bước 2 (utils là gì?): Tệp outputDir = Utils.getAlbumStorageDir (Environment.DIRECTORY_DOWNLOADS); –

+0

@DinIslamMilon, xin lỗi về điều đó, nó đã được một thời gian kể từ khi làm việc về điều này. Tôi đã thêm các lớp tĩnh utils tùy chỉnh mà tôi sử dụng. nó nhận được thư mục ảnh người dùng – wired00

Các vấn đề liên quan