【问题标题】:android media store pic from gallery来自画廊的 android 媒体商店图片
【发布时间】:2014-07-22 20:05:22
【问题描述】:

伙计

我想用从图库中选择的图像更改活动的背景

我使用资源文件夹中的图像来设置背景

View background = findViewById(R.id.background);
background.setBackgroundResource(R.drawable.phonebackground);

这会将可绘制文件夹中的图像“Phonebackground”设置为背景

我有一个方法可以让我从图库中挑选一张照片,如下所示

Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 // TODO Auto-generated method stub
 super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == RESULT_OK){
 Uri targetUri = data.getData();

这给出了格式为

的文件

content://media/external/images/media/1698

如何使用targetUri中的这些数据来设置背景

任何帮助表示赞赏

标记

编辑:

整个代码

View background = findViewById(R.id.background);
    String fileUrl = "background.txt";
    String file = android.os.Environment.getExternalStorageDirectory().getPath() +    fileUrl;
    File f = getBaseContext().getFileStreamPath(fileUrl);
    if(f.exists())
         try{
        FileInputStream fin = openFileInput(fileUrl);
             int c;
             String temp="";
             while( (c = fin.read()) != -1){
                temp = temp + Character.toString((char)c);
             }
              String asubstring = temp.substring(0, 1); 
             if(asubstring.equals("/")) 
             Drawable myDrawable = new BitmapDrawable(getResources(), temp);
             ImageView backgroundView = (ImageView) findViewById(R.id.background);
             backgroundView.setImageURI(null); 
             backgroundView.setImageDrawable(myDrawable);

             if(temp.equals("healthblue"))
             background.setBackgroundResource(R.drawable.healthblack);
             if(temp.equals("justice"))
                 background.setBackgroundResource(R.drawable.justiceblack);
             if(temp.equals("tech"))
                 background.setBackgroundResource(R.drawable.phonebackground);
             if(temp.equals("raynesback"))
                 background.setBackgroundResource(R.drawable.raynesback);
             if(temp.equals("ebbs"))
                 background.setBackgroundResource(R.drawable.ebbs);
             if(temp.equals("defenceblack"))
                 background.setBackgroundResource(R.drawable.defenceblack);
             if(temp.equals("corporate"))
                 background.setBackgroundResource(R.drawable.corporate);
             if(temp.equals("remoteblack"))
                 background.setBackgroundResource(R.drawable.remoteblack);
             if(temp.equals("prestigeblack"))
                 background.setBackgroundResource(R.drawable.prestigeblack);

             }catch(Exception e){
          }
    else{
        Toast.makeText(getBaseContext(),"NOT There",Toast.LENGTH_SHORT).show();
    }

尝试从从文本文件中获得的路径设置背景

if(asubstring.equals("/"))

任何帮助表示赞赏

标记

【问题讨论】:

标签: android background set gallery


【解决方案1】:

你可以试试这个方法:

1) 打开图库

private void selectImageFromGallery() {
    final Intent intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI );
    intent.setType( "image/*" );
    this.startActivityForResult( intent, 0 );

}

2) 获取 uri 并创建可绘制对象

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

    if ( resultCode == Activity.RESULT_OK ) {
        final Uri targetUri = data.getData();

        InputStream is;
        try {
            is = this.getContentResolver().openInputStream( targetUri );
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize = 10;
            Bitmap bitmap=BitmapFactory.decodeStream(is,null,options);

            Drawable background = new BitmapDrawable(getResources(),bitmap);

            view.setBackground(background);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }            

    }

}   

【讨论】:

  • 一旦我添加了私有 Drawable mDrawable;没有错误,但是一旦我选择了图片,它就会使应用程序崩溃。设置它的代码在 Activity2.java 中但我设置的背景在 Activity1.java 上是否有区别
  • 什么是崩溃日志?
  • soryy 我对此几乎是个新手,我如何从电话中获得它。我无法在模拟器上运行它,因为它无法访问其上的 sd 卡,所以我看不到它在哪里崩溃
  • 对不起,我不明白你的问题是什么。你能告诉我更多细节吗?
  • 我阻止它崩溃,但无法让它工作我在一个名为“temp”的字符串中有图片的路径如何将名为“background1”的布局的背景设置为图片SD卡。文件路径的格式为“/storage/emulated/0/Pictures/pic1.jpg”
【解决方案2】:

感谢以下工作的帮助

View background = findViewById(R.id.background);
    String fileUrl = "background.txt";
    String file3 = android.os.Environment.getExternalStorageDirectory().getPath(); 
    File f = getBaseContext().getFileStreamPath(fileUrl);
    if(f.exists())
         try{
        FileInputStream fin = openFileInput(fileUrl);
             int c;
             String temp="";
             while( (c = fin.read()) != -1){
                temp = temp + Character.toString((char)c);
             }
            String asubstring = temp.substring(0, 1); 
            if(asubstring.equals("/")) ;
            Resources res = getResources();
            Bitmap bitmap = BitmapFactory.decodeFile(temp);
            BitmapDrawable bd = new BitmapDrawable(res, bitmap);
            View view = findViewById(R.id.background);
            view.setBackgroundDrawable(bd);

             if(temp.equals("healthblue"))
             background.setBackgroundResource(R.drawable.healthblack);
             if(temp.equals("justice"))
                 background.setBackgroundResource(R.drawable.justiceblack);
             if(temp.equals("tech"))
                 background.setBackgroundResource(R.drawable.phonebackground);
             if(temp.equals("raynesback"))
                 background.setBackgroundResource(R.drawable.raynesback);
             if(temp.equals("ebbs"))
                 background.setBackgroundResource(R.drawable.ebbs);
             if(temp.equals("defenceblack"))
                 background.setBackgroundResource(R.drawable.defenceblack);
             if(temp.equals("corporate"))
                 background.setBackgroundResource(R.drawable.corporate);
             if(temp.equals("remoteblack"))
                 background.setBackgroundResource(R.drawable.remoteblack);
             if(temp.equals("prestigeblack"))
                 background.setBackgroundResource(R.drawable.prestigeblack);

             }catch(Exception e){
          }
    else{
        Toast.makeText(getBaseContext(),"NOT There",Toast.LENGTH_SHORT).show();
    }

标记

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多