需求很简单:我想复制一张图片然后显示出来
程序红色那行代码报错:空指针(Caused by: java.lang.NullPointerException
                                                             at com.example.administrator.imagecopy.MainActivity.onCreate(MainActivity.java:22))求助,我要疯了


主程序:package com.example.administrator.imagecopy;import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        Bitmap bmsrc = BitmapFactory.decodeFile("sdcard/a.png");
        //创建图像副本,
        //1.在内存中创建一个与原图一模一样大小的bitmap对象,创建与原图大小一致的白纸
        Bitmap bmcopy = Bitmap.createBitmap(bmsrc.getWidth(),bmsrc.getHeight(),bmsrc.getConfig());
        //创建画笔对象
        Paint paint = new Paint();
        //创建画板对象
        Canvas canvas = new Canvas(bmcopy);
        //开始作图
        canvas.drawBitmap(bmsrc,new Matrix(),paint);
        ImageView iv_src = (ImageView) findViewById(R.id.iv_src);
        ImageView iv_copy = (ImageView) findViewById(R.id.iv_copy);
        iv_src.setImageBitmap(bmsrc);
        iv_copy.setImageBitmap(bmcopy);
    }
}布局文件:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.imagecopy.MainActivity">    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_src"/>    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_copy"/></LinearLayout>

解决方案 »

  1.   

    Android6.0需要动态申请权限ActivityCompat.requestPermissions(thisActivity,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE);
      

  2.   

    bmsrc  你把这个打印一下看看是不是空的?文件地址和名字你输入对了吗
      

  3.   

    问题找到了,是因为我在清单文件中没有配置sd卡的读写权限,现在已经配置上了还有一个问题就是我每次新建一个project的时候都得去重写配去写一遍那几句sd卡权限,怎么才能让清单文件中一直都有这个权限呢??
      

  4.   


    安卓6.0以上使用的是动态权限,也就是每次运行都需要检测有没有那个权限,
    你也可以在初始界面把全部需要的权限都检测申请一遍,如果缺失某个权限用户又拒绝授权的话就退出APP,不过这种方式不太友好
      

  5.   


    安卓6.0以上使用的是动态权限,也就是每次运行都需要检测有没有那个权限,
    你也可以在初始界面把全部需要的权限都检测申请一遍,如果缺失某个权限用户又拒绝授权的话就退出APP,不过这种方式不太友好推荐一个权限管理工具给你吧:https://github.com/tbruyelle/RxPermissions/tree/master
    需要注意一点的是这个工具是基于rxjava2的
      

  6.   

        Bitmap bmcopy = Bitmap.createBitmap(bmsrc.getWidth(),bmsrc.getHeight(),bmsrc.getConfig());下次报错 把这个结构 换行
        Bitmap bmcopy =
     Bitmap.createBitmap(
    bmsrc.getWidth()
    ,bmsrc.getHeight()
    ,bmsrc.getConfig());