无法获取bitmoji图像以在Android中的intent中显示在ImageView中

时间:2016-06-23 23:34:15

标签: android android-intent android-imageview

当我在Bitmoji应用程序中选择一个bitmoji时,我正在Android中使用intent来列出我的应用程序。然后它应该启动我的应用程序并在ImageView中显示bitmoji。

它启动我的应用程序,但imageView是空白的。

我收到以下错误:

  

E / BitmapFactory:无法解码流:java.io.FileNotFoundException:/storage/emulated/0/Android/data/com.bitstrips.imoji/cache/bitmoji-1441479250.png:open failed:EACCES(权限被拒绝) )

这是我的MainActivity代码:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private Bitmap bitmap;
private ImageView mImageView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mImageView = (ImageView) findViewById(R.id.imageView);

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent); // Handle text being sent
        } else if (type.startsWith("image/")) {
            handleSendImage(intent); // Handle single image being sent
        }
    }

}

void handleSendImage(Intent intent) {
    Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
    Context context = getApplicationContext();

    String selectedImagePath;
    selectedImagePath = getPath(imageUri);
    Toast.makeText(context, selectedImagePath, Toast.LENGTH_LONG).show();
    if(imageUri!=null) {
        new imageWorkerTask().execute(selectedImagePath);
    }
}

public String getPath(Uri uri)
{
    /*
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
    */
    return uri.getPath();
}

class imageWorkerTask extends AsyncTask<String, Void, Bitmap> {
    BitmapFactory.Options options = new BitmapFactory.Options();
    Resources resources = getResources();

    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    String imageType = options.outMimeType;


    @Override
    protected Bitmap doInBackground(String... paths) {
        return decodeBitmapFromFile(paths[0]);
    }

    @Override
    protected void onPostExecute(Bitmap result) {

        mImageView.setImageBitmap(result);
    }
}

public static Bitmap decodeBitmapFromFile(String path) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    // Calculate inSampleSize
    //options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
 }
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
}

1 个答案:

答案 0 :(得分:0)

您应该将读取权限添加到AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />