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

Android 将图像保存到从gallery加载的手机,android,storage,image-uploading,Android,Storage,Image Uploading,我正在尝试将从多媒体资料加载的图像保存到手机存储器(本地路径)。有人能引导我进入这个领域吗 这是我如何从画廊中获得图像的 ImageView profilePicture; private Uri imageUri; String picturePath; @Override public void onCreate(Bundle savedInstanceState) { profilePicture = (ImageView) findViewById(R.id.profil

我正在尝试将从多媒体资料加载的图像保存到手机存储器(本地路径)。有人能引导我进入这个领域吗

这是我如何从画廊中获得图像的

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;
            }
        });
}
上传图像()

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


我可以将其显示在imageview上,但我需要将其存储在手机内存中,以便每次打开应用程序时,我都能够将先前上传的图像加载到图像视图中。然后,如果用户想要再次上传。先前保存的文件将被覆盖。我不想导致使用sqlite将图像存储为blob,因为我将只为我的整个应用上传一张图像。我想将其存储在本地文件路径中,如myappname/images/image.png。有什么想法吗?谢谢

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

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);

我试试这个。但是你能指出图像保存中的哪一部分是我引用当前图像的那一行吗?如你所见。我从图库中获得的图像直接存储在imageview中。这条线。profilePicture.setImageBitmap(BitmapFactory.decodeFile(picturePath));是位图。压缩我的图像吗?因此,我应该将BitmapFactory.decodeFile(picturePath)存储到位图中,以用于ex.bm。然后在压缩中使用它,例如:bm.compress?这是您的位图:BitmapFactory.decodeFile(picturePath));所以试着设置:BitmapFactory.decodeFile(picturePath)).compress(Bitmap.CompressFormat.PNG,100,out);我将其存储到位图变量中,以使其更有条理+一个简单的回答。正是我需要的。没有其他不必要的代码和解决方法。直截了当。谢谢跟进问题。如果我尝试上传另一张图片。这会覆盖当前保存在应用程序缓存目录中的现有文件吗?
String fname = "profile.png";
Bitmap profile = BitmapFactory.decodeFile(getCacheDir().getAbsolutePath()+ "/images/"  + fname);