2

我想通过以下方式将图像设置为 ImageView

1)当点击按钮图片库打开

2)选择任何图像并设置为图像视图并将该图像路径存储到文本文件

我做了所有这些工作,但是当应用程序关闭时,重新启动时,ImageView 不会通过从文本文件中检索图像路径来显示上一个图像

代码低于 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    img = (ImageView) findViewById(R.id.imageView1);
    ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT); 
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
        }

    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
        if (requestCode == SELECT_PICTURE) { 
            Uri selectedImageUri = data.getData(); 
            selectedImagePath = getPath(selectedImageUri); 
            System.out.println("Image Path : " + selectedImagePath); 
            img.setImageURI(selectedImageUri); 
            File myFile = new File("/sdcard/ImageLocation.txt"); 
            try { 
                myFile.createNewFile(); 
                FileOutputStream fOut = new FileOutputStream(myFile); 
                OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
                myOutWriter.append(selectedImagePath); 
                myOutWriter.close(); 
                fOut.close(); 
                Toast.makeText(getBaseContext(),"Done writing SD ",Toast.LENGTH_SHORT).show();
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace();
            } 
        }
    }
}

public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 

    return cursor.getString(column_index); 
}
4

1 回答 1

1

您在哪里阅读重新启动时的 ImagePath?请显示相同的代码。此外,在将 ImagePath 写入文本文件时,您应该执行以下操作

File myFile = new File(Environment.getExternalStorageDirectory(), "ImageLocation.txt"); 
    try { 
        if(!myFile.exists()) {
            myFile.createNewFile();
        }
        FileOutputStream fOut = new FileOutputStream(myFile); 
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
        myOutWriter.append(selectedImagePath); 
        myOutWriter.close(); 
        fOut.close(); 
    } catch (IOException e) { 
        e.printStackTrace();
    }

在阅读文件时,您还应该阅读使用Environment.getExternalStorageDirectory()

编辑:

如果您的意图只是查看图片库,那么您不应该使用intent.setAction(Intent.ACTION_GET_CONTENT);,请将其更改为intent.setAction(Intent.ACTION_PICK);.

现在,下面将是您的文件路径读取功能(您的文件写入代码将始终覆盖现有的存储路径..所以您将始终只存储一个路径....如果您希望存储多个图像路径,那么您应该修改你的文件写入和下面的文件读取部分)。

public String getStoredPath() {
    String path = null;
    File myFile = new File(Environment.getExternalStorageDirectory(), "ImageLocation.txt"); 
    if(myFile.exists()) {
        //Read text from file
        try { 
            BufferedReader br = new BufferedReader(new FileReader(myFile)); 
            String line; 
            // Below while loop will only run once as your file writing 
            // always overwrites the existing line
            while ((line = br.readLine()) != null) { 
                path = line;
            } 
        } catch (IOException e) { 

        } 
    }

    return path;
} 

然后在要设置图像uri的地方。

String imagePath = getStoredPath();
if(imagePath != null) {
    img.setImageURI(Uri.parse(imagePath));
}

请务必对文件写入进行建议的更改。此外,如果您只想存储和获取一个图像路径,请考虑使用共享首选项。

于 2013-07-29T09:44:06.477 回答