如何在应用程序设置中存储图像路径?

用户名

您好,我做到了,以便用户在我的应用程序中通过单击将他们定向到他们的画廊的按钮来选择个人资料图片。他们选择了图片,这很好,但是一旦我退出该应用程序或从多任务中删除,该图片就消失了。我该如何保存?我知道我需要共享首选项,但是我不确定如何在代码中使用它。我是初学者。

这是我目前在main.java上的代码。它显示了我如何使用代码从画廊中选择一张照片,但需要保存该照片:

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
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 {

    @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);
            }
        });;


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

        }


    }
}
泰因

将图像路径保存在SharedPreference中

SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("profilePic", picturePath);
edit.commit();

下次活动开始时,请检查您的SharedPreference。如果存在,则设置图像。以下代码可能会写在onCreate()中。

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String picturePath = prefs.getString("profilePic", "");
if(!picturePath.equals("")){
      ImageView imageView = (ImageView) findViewById(R.id.imageView1);
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在离子应用程序中设置默认图像

如何在C#中从Windows窗体应用程序项目设置.dll文件的路径?

如何在Tomcat 7.0中设置Web应用程序的上下文路径

如何在我的Web应用程序中设置相对路径

如何在我的应用程序上显示存储在移动设备中的图像?

如何在Windows RT应用程序中存储用户设置

如何在Xamarin.Forms中存储应用程序设置

如何在UI5应用程序中设置背景图像并使之响应?

如何在 Xamarin.IOS 应用程序的 UIButton 中设置背景图像?

如何在WPF应用程序中设置和放置背景图像

如何在WPF应用程序中设置所有窗口的背景图像

如何在.NET控制台应用程序中获取应用程序的路径?

如何在Web应用程序中存储选项列表

如何在iOS应用程序中存储数据

如何在应用程序变量中存储dataset()?

如何在多线程应用程序中存储线程

C#如何在应用程序中存储密码

如何在 Firebase Flutter 应用程序中存储会话

如何在Angular应用程序中隐藏损坏的图像

如何在Java应用程序中显示图像

如何在WS应用程序中获取图像源

将设置存储在可执行应用程序的相对路径中

如何在Eclipse中设置Gradle Java应用程序?

如何在Android中设置应用程序范围的字体?

如何在我的应用程序中设置twilio

如何在Java应用程序中设置锁定模式

如何在Swing中全局设置应用程序的图标?

如何在golang中设置应用程序图标?

如何在通用应用程序中设置窗口的大小?