无法加载图片

时间:2018-05-27 18:13:02

标签: android android-sharedpreferences

当我尝试加载保存在手机上的图片时,我遇到了问题。

我有一个“助手班”

public class FileHelper {

    public static String saveBitmapToFile(Bitmap bitmap, Context context, String fileName) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        // write the compressed bitmap to the outputStream(bytes)
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        FileOutputStream fo = null;
        try {
            fo = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        } catch (FileNotFoundException e) {
            Toast.makeText(context, "בעיה ,", Toast.LENGTH_SHORT).show();
        }
        try {
            fo.write(bytes.toByteArray());
            fo.close();// close file output
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }
}

这是我尝试上传照片的地方:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile2);

        iv = (ImageView) findViewById(R.id.ivPic);
        btnTakePic = (Button) findViewById(R.id.btnpic);
        btnPickPicture = (Button) findViewById(R.id.btnpicpic);


        try {
            bitmap = BitmapFactory.decodeStream(this.openFileInput(PIC_FILE_NAME));
        } catch (FileNotFoundException e) {

            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cam);
        }
        iv.setImageBitmap(bitmap);
        btnTakePic.setOnClickListener(this);
        btnPickPicture.setOnClickListener(this);
    }

    public void onClick(View v) {
        Intent intent = null;
        if (v == btnTakePic) {
            intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_PICTURE);
        } else if (v == btnPickPicture) {
            Intent pickPickIntent = new Intent(Intent.ACTION_PICK);
            File picDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String picDirPath = picDir.getPath();
            Uri uData = Uri.parse(picDirPath);
            pickPickIntent.setDataAndType(uData, "image/*");
            startActivityForResult(pickPickIntent, PICK_PICTURE);

        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK)
        {
            bitmap = (Bitmap) data.getExtras().get("data");
            iv.setImageBitmap(bitmap);
            FileHelper.saveBitmapToFile(bitmap,getApplicationContext(),PIC_FILE_NAME);
            Bitmap bitmapx = iv.getDrawingCache();
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("BitmapImage", bitmapx);

        } else if (requestCode == PICK_PICTURE) {
            if (resultCode == RESULT_OK) {
                Uri URI = data.getData();
                String[] FILE = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(URI,
                        FILE, null, null, null);
                cursor.moveToFirst();
                options = new BitmapFactory.Options();
                int columnIndex = cursor.getColumnIndex(FILE[0]);
                String ImageDecode = cursor.getString(columnIndex);
                cursor.close();
                options.inSampleSize = 5;
                Bitmap bmp = BitmapFactory.decodeFile(ImageDecode, options);
                iv.setImageBitmap(bmp);

                FileHelper.saveBitmapToFile(bmp,getApplicationContext(),PIC_FILE_NAME);

                Bitmap bitmapx = iv.getDrawingCache();
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra("BitmapImage", bitmapx);
            }
        }
    }

以下是我尝试加载保存的图片的地方:

    private void updateHeader()
    {

        ImageView ivHeader = (ImageView) mNavigationView.getHeaderView(0).findViewById(R.id.ivHeader);

        try{
            bitmap= BitmapFactory.decodeStream(this.openFileInput(PIC_FILE_NAME));
            ivHeader.setImageBitmap(bitmap);
        }
        catch (FileNotFoundException e){
            Toast.makeText(getApplicationContext(),"error occured",Toast.LENGTH_LONG);
        }

    }

我必须说没有显示Toast消息,并且没有错误。应用程序没有崩溃,但图片保持不变。

1 个答案:

答案 0 :(得分:0)

确保您具有外部读取存储权限。这是how to request permission.

PS:还要确保已将该权限添加到AndroidManifest.xml