我正在尝试从图片视图中保存图片,但我得到例外(Read-only file system)
我试过这段代码,
bitmap1 = imageView.getDrawingCache(); //for retrieving the Image
public void saveOnClick(View view)
{
try
{
String filename="qrimage";
FileOutputStream out = new FileOutputStream(filename);
bitmap1.compress(Bitmap.CompressFormat.PNG, 90, out);
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getMessage()+" Not Saved!", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
用户权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 1 :(得分:0)
这是一个保存功能
void Save() {
final File myDir = new File(folder);
myDir.mkdirs();
final Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
final String fname = "StyleMe-" + n + ".png";
file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
mCBitmap.compress(CompressFormat.JPEG, 100, out);
out.flush();
out.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
Toast.makeText(getApplication(), "Image Saved", Toast.LENGTH_SHORT)
.show();
} catch (final Exception e) {
}
}
只需在按钮上点击Save();
即可。
P.S您必须修改名称和位图名称。
---将这些声明为全局
static File file;
String folder = "/sdcard/Pictures/YourFolderName";
并且不要忘记设置读取和写入存储的权限。
答案 2 :(得分:0)
试试这种方式
public void saveOnClick(View view)
{
try
{
imageView.setDrawingCacheEnabled(true); //UPDATE HERE
imageView.buildDrawingCache(); //UPDATE HERE
bitmap1 = imageView.getDrawingCache(); //UPDATE HERE
String filename="qrimage";
FileOutputStream out = new FileOutputStream(filename);
bitmap1.compress(Bitmap.CompressFormat.PNG, 90, out);
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.getMessage()+" Not Saved!", Toast.LENGTH_SHORT).show();
}
}
答案 3 :(得分:-1)
首先确保您的应用已启用存储权限:
转到设备设置&gt;设备&gt;应用程序&gt;应用程序管理器&gt;&#34;您的应用&#34;&gt;权限&gt;启用存储权限!
清单中的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
因此,如果您想在文件存储中创建自己的目录,可以使用somethibng,如:
String myDate = getCurrentDateAndTime();
FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName");
dir.mkdirs();
String fileName = "YourFileName_"+myDate+".jpg";
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
//to refresh the gallery
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
}
希望这有帮助!