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

前端 未结 3 1083
死守一世寂寞
死守一世寂寞 2020-12-28 09:46

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.getExternalS         


        
相关标签:
3条回答
  • 2020-12-28 09:55

    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;
        }
    
    0 讨论(0)
  • 2020-12-28 09:55

    put this code in your button click listener it will redirect you into app and make sure your device have installed Instagram app.

    String type = "image/*";
    imageview.buildDrawingCache();
    Bitmap bmap = imageview.getDrawingCache();
    Uri bmpUri = getLocalBitmapUri(bmap);
    Intent share = new Intent(Intent.ACTION_SEND);
    if (Utils.isPackageExisted(this,"com.instagram.android")) {
     share.setPackage("com.instagram.android");
    }
    share.setType(type);
    share.putExtra(Intent.EXTRA_STREAM, bmpUri);
    startActivity(Intent.createChooser(share, "Share to"));
    
    0 讨论(0)
  • 2020-12-28 10:05

    Try the following code:

    File mFileImagePath = " /storage/emulated/0/Image Editor/Media/FilterImages/Image_Editor_1547816365839.jpg  ";  // Just example you use file URL
    
    private boolean checkAppInstall(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            //Error
        }
    
        return false;
    }
    
    
    private void shareInstagram(File mFileImagePath) {
        Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
        if (intent != null) {
            Intent mIntentShare = new Intent(Intent.ACTION_SEND);
            String mStrExtension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(mFileImagePath).toString());
            String mStrMimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(mStrExtension);
            if (mStrExtension.equalsIgnoreCase("") || mStrMimeType == null) {
                // if there is no extension or there is no definite mimetype, still try to open the file
                mIntentShare.setType("text*//*");
            } else {
                mIntentShare.setType(mStrMimeType);
            }
            mIntentShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFileImagePath));
            mIntentShare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    
            mIntentShare.setPackage("com.instagram.android");
            startActivity(mIntentShare);
        } else {
            Toast.makeText(mContext, "Instagram have not been installed.", Toast.LENGTH_SHORT).show();
        }
    }
    

    This code works for me and will work on all Android devices.

    0 讨论(0)
提交回复
热议问题