如何在应用程序中保存图片?

时间:2014-02-13 16:27:47

标签: android

如何设置从gallary中选择的图片。它加载正常,但当我退出应用程序时,它不再出现在那里。我尝试使用共享偏好通过复制我的代码来保存文本,这就出现了:显然我用于文本的代码不能用于图像,我不知道。我是初学者。提前谢谢

   import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


public class Main extends Activity {


    private ContextWrapper context;
    private int imagebufferlength;
    private byte[] imagebuffer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });;

这是你告诉我要输入的代码。我不得不像我说的那样制作了许多内容来解决错误:

File cacheDir = context.getCacheDir();
        File imageFile = new File(cacheDir, "image1.jpg");
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(imageFile);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.write(imagebuffer, 0, imagebufferlength);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



            Button week1 = (Button) findViewById(R.id.week1button);
            Button week2 = (Button) findViewById(R.id.week2button);
        //Button Sound
        final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.sound1);


        week1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent("com.example.timetable.WEEK1"));
            }});

                week2.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        buttonSound.start();
                        startActivity(new Intent("com.example.timetable.WEEK2"));
                    }
        });       

          };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }




private static int RESULT_LOAD_IMAGE = 1;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.imageView1);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }
}

**但是,如果我把它放在这里,它会让我这样做,我得到两个错误,我可以自我:*

public class Main extends Activity {

private ContextWrapper context;
File cacheDir = context.getCacheDir();
File imageFile = new File(cacheDir, "image1.jpg");
FileOutputStream out = new FileOutputStream(imageFile);
out.write(imagebuffer, 0, imagebufferlength);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });;

1 个答案:

答案 0 :(得分:0)

为什么不能将图像存储在应用程序缓存目录中。

String cacheDir = context.getCacheDir();
File imageFile = new File(cacheDir, "image1.jpg");
FileOutputStream out = new FileOutputStream(imageFile);
out.write(imagebuffer, 0, imagebufferlength);

当您需要使用此图像时,您可以从缓存目录中读取并使用它。

请参阅以下链接以获取更多信息。

http://developer.android.com/training/basics/data-storage/files.html