我正在努力创建一个非常简单的应用程序,允许用户在提供的图像上书写并将其保存到他们的图库中。 我尝试了下面的代码,但无法保存。 你能告诉我我需要编辑哪个部分吗?
ImageView imagecp = (ImageView) findViewById(R.id.imageView1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.actionbar, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.savebutton:
Bitmap bitmap = ((BitmapDrawable)imagecp.getDrawable()).getBitmap();
Save savefile = new Save();
savefile.SaveImage(this, bitmap);
Toast.makeText(getApplicationContext(),"image saved", Toast.LENGTH_SHORT).show();
default:
return super.onOptionsItemSelected(item);
}
public class Save {
private Context TheThis;
private String NameOfFolder = "/Amir_Paint";
private String NameOfFile = "APaintImage";
public void SaveImage(Context context,Bitmap ImageToSave){
TheThis = context;
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+ NameOfFolder;
String CurrentDateAndTime= getCurrentDateAndTime();
File dir = new File(file_path);
if(!dir.exists()){
dir.mkdirs();
}
File file = new File(dir, NameOfFile +CurrentDateAndTime+ ".jpg");
try {
FileOutputStream fOut = new FileOutputStream(file);
ImageToSave.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MakeSureFileWasCreatedThenMakeAvabile(file);
AbleToSave();
}
catch (FileNotFoundException e) {UnableToSave();}
catch (IOException e){UnableToSave();}
}
private void MakeSureFileWasCreatedThenMakeAvabile(File file) {
MediaScannerConnection.scanFile(TheThis,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage", "Scanned " + path + ":");
Log.e("ExternalStorage", "-> uri=" + uri);
}
});
}
private String getCurrentDateAndTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String formattedDate = df.format(c.getTime());
return formattedDate;
}
private void UnableToSave() {
Toast.makeText(TheThis, "Picture cannot saved to gallery", Toast.LENGTH_SHORT).show();
}
private void AbleToSave() {
Toast.makeText(TheThis, "Picture saved successfully", Toast.LENGTH_SHORT).show();
}
}
以下错误消息。
09-30 14:30:30.311 3847-3863 W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-30 14:30:30.311 3847-3863/ W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad761380, error=EGL_SUCCESS
09-30 14:30:32.259 3847-3863/ E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab79e440
09-30 14:30:38.354 3847-3847/ W/art﹕ Long monitor contention event with owner method=void android.os.MessageQueue.nativeWake(long) from MessageQueue.java:4294967294 waiters=0 for 618ms
09-30 14:30:38.389 3847-4029/ W/art﹕ Long monitor contention event with owner method=void android.os.MessageQueue.nativeWake(long) from MessageQueue.java:4294967294 waiters=1 for 651ms
09-30 14:30:38.461 3847-3863/ E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab79e360
09-30 14:32:33.654 3847-3854/ W/art﹕ Suspending all threads took: 16.292ms
这是我对清单文件的许可。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 0 :(得分:1)
您应该在onCreateView方法中设置视图实例。
这是错误的。
ImageView imagecp = (ImageView) findViewById(R.id.imageView1);
这是正确的:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.testclassfragment, container, false);
imagecp = (ImageView)v.findViewById(R.id.imageView1);
return v
}}
更改此
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+ NameOfFolder;
带
String file_path = Environment.getExternalStorageDirectory().toString() + NameOfFolder;
答案 1 :(得分:0)
请考虑使用MediaStore insertImage代替此旧代码。