将图像保存到从图库加载的手机

时间:2013-07-31 13:45:13

标签: android storage image-uploading

我正在尝试将从图库加载的图像保存到手机记忆库(本地路径)。任何人都可以指导我这个吗?

这是我从图库中获取图片的方式。

ImageView profilePicture;
private Uri imageUri;
String picturePath;

@Override
public void onCreate(Bundle savedInstanceState)  
{
     profilePicture = (ImageView) findViewById(R.id.profile_picture);
        profilePicture.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    break;
                }
                case MotionEvent.ACTION_UP:{
                    uploadImage();
                    break;
                }
                }
                return true;
            }
        });
}

uploadImage()

Intent galleryIntent = new Intent(Intent.ACTION_PICK,     android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
startActivityForResult(galleryIntent, 1);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case 0:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri;
                getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();
                Bitmap bitmap;
                try {
                     bitmap = android.provider.MediaStore.Images.Media
                     .getBitmap(cr, selectedImage);

                     profilePicture.setImageBitmap(bitmap);
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                            .show();
                    Log.e("Camera", e.toString());
                }
            }
        case 1:
             if (resultCode == Activity.RESULT_OK && null != data) {
                 Uri selectedImage = data.getData();
                 String[] filePathColumn = { MediaStore.Images.Media.DATA };

                 Cursor cursor = getContentResolver().query(selectedImage,
                         filePathColumn, null, null, null);
                 cursor.moveToFirst();

                 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                 picturePath = cursor.getString(columnIndex);
                 cursor.close();
                 profilePicture.setBackgroundColor(Color.TRANSPARENT);
                     profilePicture.setImageBitmap(BitmapFactory.decodeFile(picturePath));

             }

        }
    }

*注意:案例0用于使用手机摄像头拍摄图像。

我可以在我的imageview上显示它,但是我需要将它存储在手机的内存中,所以每当我打开应用程序时,我都可以将之前上传的图像加载到图像视图中。然后,如果用户想要再次上传。之前保存的文件将被覆盖。我不希望使用sqlite将图像存储为blob,因为我将为我的整个应用上传一个图像。我想将它存储在本地文件路径中,如myappname / images / image.png。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

您可以将图像存储在应用程序缓存目录中,例如:

try {
    String destFolder = getCacheDir().getAbsolutePath()+ "/images/";
        if (!new File(destFolder).exists()) {
            new File(destFolder).mkdirs();
        }

        FileOutputStream out = new FileOutputStream(destFolder + "profile.png");    
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.close();
} catch (Exception ex) {
    ex.printStackTrace();               
}

将文件读回Bitamp:

String fname = "profile.png";
Bitmap profile = BitmapFactory.decodeFile(getCacheDir().getAbsolutePath()+ "/images/"  + fname);
相关问题