从Android中的图库中选择图片时出现强度问题

时间:2016-07-20 09:58:59

标签: android android-intent bitmap gallery image-gallery

我知道如何从图库中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释放287K,13%免费3182K / 3640K,暂停2ms,   总计2分07-20 06:16:10.411 12940-12940 / com.example.softeng.jogifinal   I / dalvikvm:找不到方法   android.content.res.Resources.getDrawable,从方法引用   android.support.v7.widget.ResourcesWrapper.getDrawable 07-20   06:16:10.411 12940-12940 / com.example.softeng.jogifinal W / dalvikvm:   VFY:无法解析虚方法442:   Landroid /内容/ RES /资源; .getDrawable   (ILandroid /内容/ RES /资源$主题;)Landroid /图形/绘制/可绘制;   07-20 06:16:10.411 12940-12940 / com.example.softeng.jogifinal   D / dalvikvm:VFY:替换操作码0x6e在0x0002 07-20 06:16:10.411   12940-12940 / com.example.softeng.jogifinal I / dalvikvm:找不到   方法android.content.res.Resources.getDrawableForDensity,引用   从方法   android.support.v7.widget.ResourcesWrapper.getDrawableForDensity 07-20   06:16:10.411 12940-12940 / com.example.softeng.jogifinal W / dalvikvm:   VFY:无法解析虚方法444:   Landroid /内容/ RES /资源; .getDrawableForDensity   (IILandroid /内容/ RES /资源$主题;)Landroid /图形/绘制/可绘制;   07-20 06:16:10.411 12940-12940 / com.example.softeng.jogifinal   D / dalvikvm:VFY:替换操作码0x6e在0x0002 07-20 06:16:10.411   12940-12940 / com.example.softeng.jogifinal E / dalvikvm:找不到   class&#39; android.graphics.drawable.RippleDrawable&#39;,引用自   方法   android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering   07-20 06:16:10.411 12940-12940 / com.example.softeng.jogifinal   W / dalvikvm:VFY:无法解析152的实例   (Landroid / graphics / drawable / RippleDrawable;)in   Landroid /支持/ V7 /空间/ AppCompatImageHelper; 07-20 06:16:10.411   12940-12940 / com.example.softeng.jogifinal D / dalvikvm:VFY:替换   操作码0x20在0x000c 07-20 06:16:10.721   12940-12940 / com.example.softeng.jogifinal D / OpenGLRenderer:启用   调试模式0 07-20 06:16:14.441   12940-12940 / com.example.softeng.jogifinal I / dalvikvm:找不到   方法android.content.Context.getColorStateList,引用自   方法   android.support.v7.content.res.AppCompatResources.getColorStateList   07-20 06:16:14.441 12940-12940 / com.example.softeng.jogifinal   W / dalvikvm:VFY:无法解析虚方法287:   Landroid /内容/上下文; .getColorStateList   (I)Landroid /内容/ RES / ColorStateList; 07-20 06:16:14.441   12940-12940 / com.example.softeng.jogifinal D / dalvikvm:VFY:替换   操作码0x6e在0x0006 07-20 06:16:15.731   12940-12940 / com.example.softeng.jogifinal D / dalvikvm:GC_FOR_ALLOC   释放270K,12%免费3281K / 3724K,暂停3ms,总共3ms 07-20   06:16:15.741 12940-12940 / com.example.softeng.jogifinal D / dalvikvm:   GC_FOR_ALLOC释放97K,14%免费3337K / 3880K,暂停1ms,总计2ms   07-20 06:16:15.741 12940-12940 / com.example.softeng.jogifinal   I / dalvikvm-heap:将堆(frag case)增长到5.106MB(1764372字节)   分配07-20 06:16:15.751   12940-12948 / com.example.softeng.jogifinal D / dalvikvm:GC_FOR_ALLOC   释放<1K,10%免费5060K / 5604K,暂停5ms,总计5ms

我做了什么:

  1. 尝试clean项目。
  2. 尝试Invalidate and caches
  3. 重启Andriod Studio 3次。但是没有工作..
  4. 任何帮助都要得到赞赏..

1 个答案:

答案 0 :(得分:0)

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

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