Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

share - How to send a photo to Instagram using my Android app?

My app takes photos and I want to share it on Instagram.

My app save the image in this directory

File storagePath = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/tubagram");

Now I'm trying to get the last picture I took to share in Instagram using this code

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");

final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_TAKEN};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");

if (c1.moveToFirst() ) {

    Log.i("Test", "last picture (" + c1.getString(0) + ") taken on: " + new Date(c1.getLong(1)));
}

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+Environment.getExternalStorageDirectory() + "/DCIM/Camera/tubagram/" + c1.getString(0)));
shareIntent.setPackage("com.instagram.android");

c1.close();

startActivity(shareIntent);

I receive a Toast with this error message "Unable to download file". This Toast is sent by Instagram.

I tried to use this link example - share a photo in instagram - but didn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved my problem.

I add this line after the camera.takePicture.

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

This line does a "refresh" and after the phone recognizes the news photos saved on your phone.

And I made some changes on my method

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/*");                 

final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {
    MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.TITLE, MediaStore.Images.ImageColumns.DATE_TAKEN
};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");

if (c1.moveToFirst() ) {
    Log.i("Test", "last picture (" + c1.getString(1) + ") taken on: " + new Date(c1.getLong(2)));
}

Log.i("Image path", "file://"+Environment.getExternalStorageDirectory()+ "/Tubagram/"  + c1.getString(1) + ".png");

shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+Environment.getExternalStorageDirectory()+ "/Tubagram/" + c1.getString(1)+".png"));
shareIntent.setPackage("com.instagram.android");

c1.close();

startActivity(shareIntent);

And with this another method I verify if the Instagram is installed on the phone

private boolean verifyInstagram(){
    boolean installed = false;

    try {
        ApplicationInfo info = getPackageManager().getApplicationInfo("com.instagram.android", 0);
        installed = true;
    } catch (NameNotFoundException e) {
        installed = false;
    }
        return installed;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...