将位图保存到内部存储并通过intent android共享

时间:2013-06-06 10:54:09

标签: android

我将位图图像(bm)转换为jpeg并使用

将图像存储在内部手机存储器中
    ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, outputBuffer);
    byte[] byteImage1=outputBuffer.toByteArray();


    //save file to internal storage
    FileOutputStream outputStream;

    try {
      outputStream = context.openFileOutput("myphoto.jpg", Context.MODE_PRIVATE);
      outputStream.write(byteImage1);
      outputStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    }

现在我想使用shareIntent

分享存储的图像
Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");
            share.putExtra(Intent.EXTRA_STREAM,
                    Uri.parse(context.getFilesDir().getPath() + "myphoto.jpg"));
            context.startActivity(Intent.createChooser(share, "Partager par"));

但按下点击按钮后意图没有运行。请在这个问题上提供帮助。

1 个答案:

答案 0 :(得分:0)

您需要编写Content Providers以在内部存储空间中共享图像

请参阅Sharing images that are stored on internal memory

public class MediaContentProvider extends ContentProvider {
private static final String TAG = "MediaContentProvider";

// name for the provider class
public static final String AUTHORITY = "com.way.srl.HandyWay.contentproviders.media";

private MediaData _mediaData;

// UriMatcher used to match against incoming requests
private UriMatcher _uriMatcher;

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public String getType(Uri uri) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    // TODO Auto-generated method stub
    return null;
}

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

    // Add a URI to the matcher which will match against the form
    // 'content://com.stephendnicholas.gmailattach.provider/*'
    // and return 1 in the case that the incoming Uri matches this pattern
    _uriMatcher.addURI(AUTHORITY, "*", 1);

    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    return 0;
}

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

    Log.v(TAG, "Called with uri: '" + uri + "'." + uri.getLastPathSegment());

    // Check incoming Uri against the matcher
    switch (_uriMatcher.match(uri)) {

    // If it returns 1 - then it matches the Uri defined in onCreate
        case 1:

            // The desired file name is specified by the last segment of the
            // path
            // E.g.
            // 'content://com.stephendnicholas.gmailattach.provider/Test.txt'
            // Take this and build the path to the file
            // String fileLocation = getContext().getCacheDir() + File.separator + uri.getLastPathSegment();
            Integer mediaID = Integer.valueOf(uri.getLastPathSegment());

            if (_mediaData == null) {
                _mediaData = new MediaData();
            }
            Media m = _mediaData.get(mediaID);

            // Create & return a ParcelFileDescriptor pointing to the file
            // Note: I don't care what mode they ask for - they're only getting
            // read only
            ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(m.filePath), ParcelFileDescriptor.MODE_READ_ONLY);
            return pfd;

            // Otherwise unrecognised Uri
        default:
            Log.v(TAG, "Unsupported uri: '" + uri + "'.");
            throw new FileNotFoundException("Unsupported uri: " + uri.toString());
    }
}