在使用GridView时,resolveuri在坏位图uri上失败了

时间:2013-01-10 22:54:53

标签: android gridview bitmap thumbnails

您好我正在尝试开发Android应用程序,从手机摄像头中提取所有图像并在GridView中显示。但是,我收到以下错误:

01-11 09:38:38.890: I/System.out(6766): resolveUri failed on bad bitmap uri:   /storage/sdcard0/DCIM/.thumbnails/1347279747819.jpg

事实上我得到了很多,这就是为什么当我在手机上测试它时,我可以看到滚动条但没有显示缩略图(除了2张图像)

这是我认为相关的代码:

package org.dale.dalegetcontent1;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class GetContent1 extends FragmentActivity  implements LoaderCallbacks<Cursor>{

/** SimpleCursorAdapter, holds images and layout for the gridview */
SimpleCursorAdapter mAdapter;

@Override
protected void onStart() {
    super.onStart();

    /** Initializes the Loader */
    getSupportLoaderManager().initLoader(0, null, this);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_content1);

    /** Getting a reference to gridview of the MainActivity layout */
    GridView gridView = (GridView) findViewById(R.id.gridview);

    /** Create an adapter for the gridview */
    /** This adapter defines the data and the layout for the grid view */
    mAdapter = new SimpleCursorAdapter(
        getBaseContext(),
        R.layout.gridview,
        null,
        new String[] { "_data","_id"} ,
        new int[] { R.id.img},
        0
    );

    /** Setting adapter for the gridview */
    gridView.setAdapter(mAdapter);

    /** Loader to get images from the SD Card */
    getSupportLoaderManager().initLoader(0, null, this);

    /** Defining item click listener for the grid view */
    OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

            /** Getting the cursor object corresponds to the clicked item */
            Cursor c1 = (Cursor ) arg0.getItemAtPosition(position);

            /** Getting the image_id from the cursor */
            /** image_id of the thumbnail is same as the original image id */
            String id = c1.getString(c1.getColumnIndex("image_id"));

            /** Creating a bundle object to pass the image_id to the ImageDialog */
            Bundle b = new Bundle();

            /** Storing image_id in the bundle object */
            b.putString("image_id", id);

            /** Instantiating ImageDialog, which displays the clicked image */
            ImageDialog img = new ImageDialog();

            /** Setting the bundle object to the ImageDialog */
            img.setArguments(b);

            /** Opening ImageDialog */
            img.show(getSupportFragmentManager(), "IMAGEDIALOG");
           return ;
        }

    };

    /** Setting itemclicklistener for the grid view */
    gridView.setOnItemClickListener(itemClickListener);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_get_content1, menu);
    return true;
}

/** A callback method invoked by the loader when initLoader() is called */
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    /** Getting uri to the Thumbnail images stored in the external storage */
    Uri uri = MediaStore.Images.Thumbnails.getContentUri("external");

    System.out.println("Thumb URI: "+uri.toString());

    /** Invoking the uri */
    return new CursorLoader(this, uri, null, null, null, null);
}

/** A callback method, invoked after the requested content provider returned all the data */
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    mAdapter.swapCursor(arg1);
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    // TODO Auto-generated method stub
}
}

我的gridview.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
    android:id="@+id/img"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/img_description"
    android:padding="10dp"
    android:adjustViewBounds="true"
/>
</LinearLayout>

我的activity_get_content1.xml在布局下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GetContent1" >

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
/>

</RelativeLayout>

谢谢,很高兴在需要时提供更多信息

干杯,戴尔

1 个答案:

答案 0 :(得分:0)

也许您应该在AndroidManifest.xml中添加此权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>