android保存图片问题
我最近写了一个非常小的程序,把一个指定数组生成幅图片并保存在sd卡上,但不知道是什么原因在执行压缩那句话时,程序抛出了,而且sd卡上也没有相应的文件夹生成。哪位大神能告诉...
我最近写了一个非常小的程序,把一个指定数组生成幅图片并保存在sd卡上,但不知道是什么原因在执行压缩那句话时,程序抛出了,而且sd卡上也没有相应的文件夹生成。哪位大神能告诉我是怎么回事。
public class SaveActivity extends Activity {
/** Called when the activity is first created. */
public Button btn;
public ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img=(ImageView)findViewById(R.id.img);
btn=(Button)findViewById(R.id.btn);
onclick();
}
public void onclick(){
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveImage();
}
private void saveImage() {
// TODO Auto-generated method stub
int [] a= new int [16384];
for(int i =0;i<16384;i++)
a[i] = -1601933;
Bitmap bmp = Bitmap.createBitmap(a, 0,128, 128, 128,Bitmap.Config.RGB_565);
img.setImageBitmap(bmp); File filename;
try {
String path = Environment.getExternalStorageDirectory().toString();
new File(path + "/folder/subfolder").mkdirs();
filename = new File(path + "/folder/subfolder/image.jpg");
FileOutputStream out = new FileOutputStream(filename);
[color=#FF0000]boolean m = bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);[/color]
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
filename.getAbsolutePath(), filename.getName(),
filename.getName());
Toast.makeText(getApplicationContext(),
"File is Saved in " + filename, 1000).show();
} catch (Exception e) {
e.printStackTrace();
System.out.println("s;dlfkjg");
}
}
});
}
另外权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>已经加上了,不是权限的问题 展开
public class SaveActivity extends Activity {
/** Called when the activity is first created. */
public Button btn;
public ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img=(ImageView)findViewById(R.id.img);
btn=(Button)findViewById(R.id.btn);
onclick();
}
public void onclick(){
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveImage();
}
private void saveImage() {
// TODO Auto-generated method stub
int [] a= new int [16384];
for(int i =0;i<16384;i++)
a[i] = -1601933;
Bitmap bmp = Bitmap.createBitmap(a, 0,128, 128, 128,Bitmap.Config.RGB_565);
img.setImageBitmap(bmp); File filename;
try {
String path = Environment.getExternalStorageDirectory().toString();
new File(path + "/folder/subfolder").mkdirs();
filename = new File(path + "/folder/subfolder/image.jpg");
FileOutputStream out = new FileOutputStream(filename);
[color=#FF0000]boolean m = bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);[/color]
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
filename.getAbsolutePath(), filename.getName(),
filename.getName());
Toast.makeText(getApplicationContext(),
"File is Saved in " + filename, 1000).show();
} catch (Exception e) {
e.printStackTrace();
System.out.println("s;dlfkjg");
}
}
});
}
另外权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>已经加上了,不是权限的问题 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询