【问题标题】:How to send a photo to Instagram using my Android app?如何使用我的 Android 应用将照片发送到 Instagram?
【发布时间】:2013-05-31 19:04:07
【问题描述】:

我的应用拍照,我想在 Instagram 上分享。

我的应用程序将图像保存在此目录中

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

现在我正在尝试使用此代码获取我在 Instagram 中分享的最后一张照片

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

我收到带有“无法下载文件”错误消息的 Toast。 此 Toast 由 Instagram 发送。

我尝试使用此链接示例 - share a photo in instagram - 但没有用。

【问题讨论】:

    标签: android share photo instagram


    【解决方案1】:

    我解决了我的问题。

    我在 camera.takePicture 之后添加了这一行。

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

    在手机识别到您手机上保存的新闻照片后,此行会“刷新”。

    我对我的方法做了一些改变

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

    通过另一种方法,我可以验证手机上是否安装了 Instagram

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

    【讨论】:

    • 请尽量始终使用英文的方法和变量,这样更便于大家理解你要做什么。
    • 您是否还可以将您的应用名称与您的图像一起发送,以便其他人可以识别此内容是通过其他应用而不是直接通过 Instagram 发送的
    【解决方案2】:

    将此代码放入您的按钮单击侦听器中,它会将您重定向到应用程序并确保您的设备已安装 Instagram 应用程序。

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

    【讨论】:

    • 什么是getLocalBitmapUri
    • getLocalBitmapUri 是从位图图像中给出 uri 的函数
    • 这里是代码:. public Uri getLocalBitmapUri(Bitmap bmp) { Uri bmpUri = null; try { File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png"); FileOutputStream out = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 90, out); out.close(); bmpUri = Uri.fromFile(file); } catch (IOException e) { e.printStackTrace(); } return bmpUri; }
    【解决方案3】:

    试试下面的代码:

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

    此代码适用于我,适用于所有 Android 设备。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多