在 Android 中从图库中选择图像时出现强度问题

Strength issue while pick Image from gallery in Android

我知道如何从 gallery.But pickUp Image 相同的代码在我的其他应用程序中工作但在我的新应用程序中不起作用。

public class RegisterActivity extends AppCompatActivity{

    Button upload_proof_button;

    Bitmap bitmaps;
    private static final int PICK_IMAGE = 100;
    ImageView filename;

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

        filename = (ImageView) findViewById(R.id.filename_textveiw);

        upload_proof_button = (Button)findViewById(R.id.upload_proof_button);
        upload_proof_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent gallery =  new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                startActivityForResult(gallery, PICK_IMAGE);
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
            Uri imageUri = data.getData();

            try {
                bitmaps = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);

                if(bitmaps == null){
                    filename.setImageBitmap(bitmaps);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}

我在 manifest.xml 文件中写入许可。

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

错误Logcat:

07-20 06:16:10.401 12940-12940/com.example.softeng.jogifinal D/dalvikvm: GC_FOR_ALLOC freed 287K, 13% free 3182K/3640K, paused 2ms, total 2ms 07-20 06:16:10.411 12940-12940/com.example.softeng.jogifinal I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable 07-20 06:16:10.411 12940-12940/com.example.softeng.jogifinal W/dalvikvm: VFY: unable to resolve virtual method 442: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 07-20 06:16:10.411 12940-12940/com.example.softeng.jogifinal D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 07-20 06:16:10.411 12940-12940/com.example.softeng.jogifinal I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 07-20 06:16:10.411 12940-12940/com.example.softeng.jogifinal W/dalvikvm: VFY: unable to resolve virtual method 444: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 07-20 06:16:10.411 12940-12940/com.example.softeng.jogifinal D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 07-20 06:16:10.411 12940-12940/com.example.softeng.jogifinal E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 07-20 06:16:10.411 12940-12940/com.example.softeng.jogifinal W/dalvikvm: VFY: unable to resolve instanceof 152 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper; 07-20 06:16:10.411 12940-12940/com.example.softeng.jogifinal D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c 07-20 06:16:10.721 12940-12940/com.example.softeng.jogifinal D/OpenGLRenderer: Enabling debug mode 0 07-20 06:16:14.441 12940-12940/com.example.softeng.jogifinal I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList 07-20 06:16:14.441 12940-12940/com.example.softeng.jogifinal W/dalvikvm: VFY: unable to resolve virtual method 287: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList; 07-20 06:16:14.441 12940-12940/com.example.softeng.jogifinal D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006 07-20 06:16:15.731 12940-12940/com.example.softeng.jogifinal D/dalvikvm: GC_FOR_ALLOC freed 270K, 12% free 3281K/3724K, paused 3ms, total 3ms 07-20 06:16:15.741 12940-12940/com.example.softeng.jogifinal D/dalvikvm: GC_FOR_ALLOC freed 97K, 14% free 3337K/3880K, paused 1ms, total 2ms 07-20 06:16:15.741 12940-12940/com.example.softeng.jogifinal I/dalvikvm-heap: Grow heap (frag case) to 5.106MB for 1764372-byte allocation 07-20 06:16:15.751 12940-12948/com.example.softeng.jogifinal D/dalvikvm: GC_FOR_ALLOC freed <1K, 10% free 5060K/5604K, paused 5ms, total 5ms

我做了什么:

  1. 尝试 clean 项目。
  2. 尝试Invalidate and caches.
  3. 重启 Andriod Studio 3 次。但是不行..

感谢任何帮助..

您检查您的位图是否 null 以将其设置到 ImageView。更改为:

if (bitmaps != null) {
    filename.setImageBitmap(bitmaps);
} else {
    filename.setImageResource(R.drawable.default_image);
}