bircastri
bircastri

Reputation: 153

Refresh image in imageview

Below is my code:

layout - 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>

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

So I would like to select one image from my gallery then display it in ImageView. I am not getting any error but imageview is not getting refreshed.

Where is the problem?

Upvotes: 0

Views: 878

Answers (2)

bircastri
bircastri

Reputation: 153

I have solved. My code found but i have any Photo with problem, if a download a photo from internet ed select it from my application i show it.

Upvotes: 0

Ayman Mahgoub
Ayman Mahgoub

Reputation: 4210

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

Upvotes: 1

Related Questions