2013-11-28 3 views
0

Ниже мой код:Обновить изображение в ImageView

макет - setting.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Ciao" 
    /> 
<Button android:text="Browse gallery" 
    android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</Button> 
<ImageView android:id="@+id/ImageView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</ImageView> 
</LinearLayout> 

класс - Setting.java

package com.mcSolution.setting; 

import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.mcSolution.R; 
import com.mcSolution.beans.Program; 


public class setting extends abstractSetting{ 
    public ImageView targetImage; 
    private static final int SELECT_PICTURE = 1; 

    private String selectedImagePath; 
    private ImageView img; 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.setting); 

     img = (ImageView)findViewById(R.id.ImageView01); 

     ((Button) findViewById(R.id.Button01)) 
       .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 <span class='IL_AD' id='IL_AD4'>Picture</span>"), 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); 
       img.refreshDrawableState(); 

      } 
     } 
    } 

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

Так что я хотел бы, чтобы выбрать одно изображение из моего затем отобразите его в ImageView. Я не получаю никаких ошибок, но изображение не обновляется.

Где проблема?

+0

приведенный выше код отлично работает в моей таблетке нексуса – prakash

ответ

1
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inPreferredConfig = Bitmap.Config.ARGB_8888; 

Bitmap photoRespresntation = BitmapFactory.decodeFile(imagePath, options); 
String photoEncoding = BitmapUtils.encodeImage(photoRespresntation); 

ImageView image = (ImageView) this.findViewById(R.id.photo); 
image.setImageBitmap(photoEncoding); 
0

Я решил. Мой код найден, но у меня есть фото с проблемой, если вы загружаете фотографию из Интернета, выберите ее из моего приложения, я покажу ее.

Смежные вопросы