【问题标题】:Can't get bitmoji image to display in ImageView from intent in android无法从android中的意图获取bitmoji图像以在ImageView中显示
【发布时间】:2016-06-24 07:55:41
【问题描述】:

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

它可以很好地启动我的应用程序,但 imageView 是空白的。

我收到以下错误:

E/BitmapFactory:无法解码流:java.io.FileNotFoundException:/storage/emulated/0/Android/data/com.bitstrips.imoji/cache/bitmoji-1441479250.png:打开失败: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);
}

【问题讨论】:

  • 我能够从资源(从我的可绘制文件夹)中将图像加载到 imageView 中。特别是当我试图从另一个应用程序(bitmoji)获取图像时 imageView 没有更新。

标签: android android-intent android-imageview


【解决方案1】:

您应该在 AndroidManifest.xml 中添加读取权限

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

【讨论】:

  • 我的 AndroidManifest.xml 中确实有这个,一定还有其他问题...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-16
  • 1970-01-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
相关资源
最近更新 更多