onActivityResult没有给出任何结果

时间:2011-06-09 13:10:13

标签: java android image browser android-activity

我编写了一个图像浏览类,让用户可以选择图像,但我无法获得图像的任何路径。 onActivityResult没有给出任何结果。我尝试使用Log.v进行检查,但没有成功。

头等舱:

showImg = new ImageView(context);
showImg.setAdjustViewBounds(true);
showImg.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // Select image
        BrowsePicture select = new BrowsePicture(context);

        Log.v("BrowsePictureSSS", ""+select.filemanagerstring);
        Log.v("BrowsePictureSSS", ""+select.selectedImagePath);
        Bitmap bMap = BitmapFactory.decodeFile(select.filemanagerstring);
        showImg.setImageBitmap(bMap);

        }
    });

BrowseImage类:

package com.FWilson.MyFavoritePlaces;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
public class BrowsePicture extends Activity {

private static final int SELECT_PICTURE = 1;

protected String selectedImagePath;

protected String filemanagerstring;

public BrowsePicture(Activity context) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    context.startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
    setResult(0);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();

            //OI FILE Manager
            filemanagerstring = selectedImageUri.getPath();

            //MEDIA GALLERY
            selectedImagePath = getPath(selectedImageUri);

            //DEBUG PURPOSE - you can delete this if you want
            if(selectedImagePath!=null)
                Log.v("BrowsePic", ""+selectedImagePath);
            else System.out.println("selectedImagePath is null");
            if(filemanagerstring!=null)
                Log.v("BrowsePic", ""+filemanagerstring);
            else System.out.println("filemanagerstring is null");

            //NOW WE HAVE OUR WANTED STRING
            if(selectedImagePath!=null) {

            }
            else {


            }

        }
    }
}

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

3 个答案:

答案 0 :(得分:1)

您的BrowsePicture活动缺少onCreate()方法。如果完全删除构造函数并将当前的代码放在onCreate()中,则可能会有更好的结果 - 这适用于Activity重载。

答案 1 :(得分:0)

BrowsePicture构造函数中的第一个setResult(0)似乎没用。你对super.onActivity的调用也没有。

记录onActivity的参数值,看看会发生什么。

此致 斯特凡

答案 2 :(得分:0)

你应该看一下Android Activity Lifecycle as defined in the class overview for the Activity class。您将看到自己不应该实例化Activity,而是应该实现Android提供的生命周期回调。

您似乎已经正在使用其他一些可用的Android回调,(onActivityResult())您只需要处理生命周期回调。