2013-06-13 40 views
10

Tôi đang phát triển một ứng dụng Android là thư viện hình ảnh trong đó hình ảnh được tải xuống từ internet để hiển thị trên màn hình của điện thoại thông minh. Hình ảnh được hiển thị một lần và ứng dụng có một nút để chia sẻ hình ảnh được hiển thị.Chia sẻ hình ảnh với nhà cung cấp nội dung trong ứng dụng Android

Làm theo hướng dẫn tôi đã tìm thấy trong một số bài đăng trên StackOverflow cho biết cách đúng để chia sẻ hình ảnh bằng cách sử dụng Trình cung cấp nội dung mà tôi đã triển khai mã sau hoạt động để chia sẻ hình ảnh của một số ứng dụng nhất định (ví dụ: Twitter, Gmail. ..) nhưng không hoạt động với những người khác (Facebook, Yahoo, MMS ...).

Sau đó, tôi hiển thị mã được sử dụng với hy vọng bạn có thể giúp tôi thực hiện chính xác để chia sẻ hình ảnh trong tất cả các ứng dụng.

Ban đầu tôi nắm bắt những nút bấm để chia sẻ:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    if (item.getItemId() == R.id.menu_share) { 

     // I get the image being displayed on the screen 
     View root = getView(); 
     ImageView imageView = (ImageView) root.findViewById(R.id.image); 
     Drawable imageToShareDrawable = imageView.getDrawable(); 

     if (imageToShareDrawable instanceof BitmapDrawable) { 

      // I convert the image to Bitmap 
      Bitmap imageToShare = ((BitmapDrawable) imageToShareDrawable).getBitmap(); 

      // Name of de image extracted from a bean property 
      String fileName = quote.getImage(); 

      // I keep the image in the folder "files" of internal storage application 
      TempInternalStorage.createCachedFile(fileName, imageToShare, getActivity().getApplicationContext()); 

      // I start the Activity to select the application to share the image after the intent Built with the method "getDefaultShareIntent" 
      startActivity(getDefaultShareIntent(fileName)); 
     } else { 
      Toast.makeText(getActivity().getApplicationContext(), "Please wait, the quote is being downloaded", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    return true; 
} 

Phương pháp lưu ảnh vào bộ nhớ trong của ứng dụng được như sau:

public static void createCachedFile(String fileName, Bitmap image, Context context) { 

    try { 
     File file = new File(context.getFilesDir(), fileName); 

     if (!file.exists()) { 
      FileOutputStream fos = new FileOutputStream(file); 
      image.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
      fos.flush(); 
      fos.close(); 
     } 
    } catch (Exception e) { 
     Log.e("saveTempFile()", "**** Error"); 
    } 
} 

Phương pháp xây dựng mục đích? để chia sẻ nó:

private Intent getDefaultShareIntent(String fileName) { 
    final Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.setType("image/jpeg"); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Test text"); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + CachedFileProvider.AUTHORITY + File.separator + "img" + File.separator + fileName)); 
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

    return shareIntent; 
} 

Cuối cùng ContentProvider code như sau:

public class CachedFileProvider extends ContentProvider { 

private static final String CLASS_NAME = "CachedFileProvider"; 

public static final String AUTHORITY = "com.example.appname.cachefileprovider"; 

private UriMatcher uriMatcher; 

@Override 
public boolean onCreate() { 
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); 

    uriMatcher.addURI(AUTHORITY, "img/*", 1); 

    return true; 
} 

@Override 
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { 

    String LOG_TAG = CLASS_NAME + " - openFile"; 

    Log.i(LOG_TAG, "Called with uri: '" + uri + "'." + uri.getLastPathSegment()); 

    switch (uriMatcher.match(uri)) { 

    case 1: 

     String fileLocation = getContext().getFilesDir() + File.separator + uri.getLastPathSegment(); 

     ParcelFileDescriptor image = ParcelFileDescriptor.open(new File(fileLocation), ParcelFileDescriptor.MODE_READ_ONLY); 

     return image; 

    default: 
     Log.i(LOG_TAG, "Unsupported uri: '" + uri + "'."); 
     throw new FileNotFoundException("Unsupported uri: " + uri.toString()); 
    } 
} 

@Override 
public int update(Uri uri, ContentValues contentvalues, String s, String[] as) { 
    return 0; 
} 

@Override 
public int delete(Uri uri, String s, String[] as) { 
    return 0; 
} 

@Override 
public Uri insert(Uri uri, ContentValues contentvalues) { 
    return null; 
} 

@Override 
public String getType(Uri uri) { 
    return null; 
} 

@Override 
public Cursor query(Uri uri, String[] projection, String s, String[] as1, String s1) { 

    MatrixCursor c = null; 

    Log.i(">>>> projection", java.util.Arrays.toString(projection)); 

    String fileLocation = getContext().getFilesDir() + File.separator + uri.getLastPathSegment(); 

    File file = new File(fileLocation); 

    long time = System.currentTimeMillis(); 

    c = new MatrixCursor(new String[] { "_id", "_data", "orientation", "mime_type", "datetaken", "_display_name" }); 

    c.addRow(new Object[] { 0, file, 0, "image/jpeg", time, uri.getLastPathSegment() }); 

    return c; 
} 

@Override 
public String[] getStreamTypes(Uri uri, String mimeTypeFilter) { 
    return null; 
} 

}

Tôi đã phát hiện ra rằng khi hình ảnh được chia sẻ một số ứng dụng chỉ gọi phương thức "truy vấn" (đây là những nơi mã không làm việc và tôi không thể chia sẻ hình ảnh) trong khi có những người khác cũng gọi phương thức "truy vấn" cũng gọi phương thức "openFile" và các phương thức này hoạt động và tôi có thể chia sẻ hình ảnh.

Tôi hy vọng bạn có thể giúp tôi, cảm ơn bạn rất nhiều trước.

+0

Bạn đã tìm thấy giải pháp chưa? Bằng cách này liên kết này có thể là thú vị cho bạn: https://github.com/xperimental/BinaryContent – rekire

+0

có u thành công chia sẻ hình ảnh của bạn có thể xin vui lòng giúp tôi làm thế nào tôi có thể chia sẻ hình ảnh từ thư mục drawable? – Erum

+1

@Mike bạn có thể chia sẻ những gì đã xảy ra trong mã của bạn? – Erum

Trả lời

0

Khi nhận xét của @Sun Ning nhận xét một số "ứng dụng nhắm mục tiêu chia sẻ" có thể xử lý URI bắt đầu bằng "nội dung: // .." mà bạn đã triển khai.

Các ứng dụng khác xử lý tập tin uri-s bắt đầu bằng "file: // ...." vì vậy bạn phải thực hiện một menue phần 2 "chia sẻ dưới dạng file"

private Intent getFileShareIntent(String fullPathTofile) { 
    final Intent shareIntent = new Intent(); 
    shareIntent.setAction(Intent.ACTION_SEND); 
    shareIntent.setType("image/jpeg"); 
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + fullPathTofile)); 
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

    return shareIntent; 
} 

Bạn có thể sử dụng để android app intentintercept tìm hiểu "ứng dụng nguồn chia sẻ" khác cung cấp những gì.

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