我如何...将图像从imagview存储到SD卡。按一下按钮 [英] How do I...Store Image from imagview To sd card.on a button click
问题描述
嗨
好中午给所有人
我正在开发一个应用程序,它将在图像视图上显示图像。< br $> b $ b
我需要:
我需要的是当我点击按钮然后它应该存储图像存在于SD卡(模拟器)的图像视图中。
以下是我使用的方式:(但没有预期结果)
按钮btnWriteSDFile =(按钮)findViewById(R.id.btnWriteSDFile);
btnWriteSDFile.setOnClickListener(new OnClickListener(){
public void onClick(View v){
ImageView myImage =(ImageView)findViewById(R.id.imageView1);
BitmapDrawable drawable =(BitmapDrawable)myImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File( sdCardDirectory,image.png);
boolean success = false;
//将文件编码为PNG图像。
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG,100,outStream);
outStream.flush( );
outStream.close();
success = true;
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e ){
e.printStackTrace();
}
if(成功){
Toast.makeText(getApplicationContext(),图像保存成功,
Toast .LENGTH_LONG)。show();
}其他{
Toast.makeText(getApplicationContext(),
图像保存期间出错,Toast.LENGTH_LONG)。show();
}
}
});
在上面的代码中我没有收到任何错误
它只是显示在mnt / sd / image.png 中保存文件但没有找到图像。
如果有人帮我解决了这个问题,那将是很明显的。
提前致谢
试试这个...
Bitmap bitmap = drawable.getBitmap();
String mBaseFolderPath = Environment.getExternalStorageDirectory()。getAbsolutePath();
if(!new File(mBaseFolderPath).exists()){
new File(mBaseFolderPath).mkdir();
}
字符串mFilePath = mBaseFolderPath +/ image.png;
文件file = new File(mFilePath);
FileOutputStream stream = new FileOutputStream(file);
if(!file.exists()){
file.createNewFile();
}
bitmap.compress(CompressFormat.PNG,100,stream);
bitmap.recycle();
stream.flush();
stream.close();
Hi
Good Noon To all
I was Developing an application which will have image on image view .
My need:
What i need is When i click the button then it should store the image that exist in the image view to the sd card(emulator).
Here is how i used:(but no expected results)
Button btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile); btnWriteSDFile.setOnClickListener(new OnClickListener() { public void onClick(View v) { ImageView myImage = (ImageView) findViewById(R.id.imageView1); BitmapDrawable drawable = (BitmapDrawable) myImage.getDrawable(); Bitmap bitmap = drawable.getBitmap(); File sdCardDirectory = Environment.getExternalStorageDirectory(); File image = new File(sdCardDirectory, "image.png"); boolean success = false; // Encode the file as a PNG image. FileOutputStream outStream; try { outStream = new FileOutputStream(image); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close(); success = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (success) { Toast.makeText(getApplicationContext(), "Image saved with success", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Error during image saving", Toast.LENGTH_LONG).show(); } } });
In my above code i didnt get any error
It simply Shows that "save file in mnt/sd/image.png" but no images found.
It would be appreciable if some one helps me to get me out from this issue.
Thanks in advance
Try this...
Bitmap bitmap = drawable.getBitmap(); String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath(); if (!new File(mBaseFolderPath).exists()) { new File(mBaseFolderPath).mkdir(); } String mFilePath = mBaseFolderPath + "/image.png"; File file = new File(mFilePath); FileOutputStream stream = new FileOutputStream(file); if (!file.exists()){ file.createNewFile(); } bitmap.compress(CompressFormat.PNG, 100, stream); bitmap.recycle(); stream.flush(); stream.close();
这篇关于我如何...将图像从imagview存储到SD卡。按一下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!