简体   繁体   English

尝试将imageView图像保存到图库,但无法保存到图库。(Android)

[英]Trying to save imageView image to gallery, but cannot saved to gallery.(Android)

I'm trying to make a really simple app which allow users write on the image provided and save it to their gallery. 我正在尝试制作一个非常简单的应用程序,它允许用户在提供的图像上书写并将其保存到他们的画廊。 And I tried the code below, and it can't be saved. 我尝试了下面的代码,但无法保存。 Could you tell me which part I need to edit? 你能告诉我我需要编辑哪一部分吗?

 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();

}

} }

And error message below. 和下面的错误消息。

   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

This is the permission I put on menifest file. 这是我对清单文件的许可。

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

You should set the view instance inside of onCreateView method. 您应该在onCreateView方法内设置视图实例。

This is wrong. 错了

ImageView imagecp = (ImageView) findViewById(R.id.imageView1);

This is the right: 这是正确的:

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
}}

change this 改变这个

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath()+ NameOfFolder;

with

String file_path = Environment.getExternalStorageDirectory().toString() + NameOfFolder;

请考虑使用MediaStore insertImage代替此旧代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM