我如何...将图像从imagview存储到SD卡。按一下按钮 [英] How do I...Store Image from imagview To sd card.on a button click

查看:86
本文介绍了我如何...将图像从imagview存储到SD卡。按一下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




好​​中午给所有人



我正在开发一个应用程序,它将在图像视图上显示图像。< 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆