デフォルトの Android ギャラリーを使用して、特定のフォルダーに画像を表示しようとしています。これを行うためのサンプル コードは 1 つしか見つかりませんでした。私はそのコードを単純化しましたが、今では非常に明確です:
public class ScanActivity extends Activity implements MediaScannerConnectionClient {
private static final String SCAN_DIR = Environment.getExternalStorageDirectory() + File.separator + "MyFolder" + File.separator;
private static final String FILE_TYPE="image/*";
private String scanPath;
private MediaScannerConnection conn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scanPath = SCAN_DIR + new File(SCAN_DIR).list()[0];
conn = new MediaScannerConnection(getApplicationContext(), this);
conn.connect();
}
@Override
public void onMediaScannerConnected()
{
conn.scanFile(scanPath, FILE_TYPE);
}
@Override
public void onScanCompleted(String path, Uri uri)
{
try
{
if (uri != null)
{
//uri = content://media/external/images/media/11271
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, FILE_TYPE);
startActivity(intent);
}
}
finally
{
conn.disconnect();
conn = null;
}
}
}
Android Froyo (2.2)では正常に動作します - ギャラリーはMyFolder
ディレクトリ内のすべての画像を開きますが、Jelly Bean (4.1)では黒い画面しか表示されません (下のスクリーンショットを参照)。もちろん、スキャンしたフォルダには両方のデバイスの写真が含まれています。
logcat の出力:
11-03 18:31:31.872: W/DataManager(12540): exception in creating media object: /local/image/item/11271
11-03 18:31:31.872: W/DataManager(12540): java.lang.RuntimeException: cannot find data for: /local/image/item/11271
11-03 18:31:31.872: W/DataManager(12540): at com.android.gallery3d.data.LocalImage.<init>(LocalImage.java:104)
11-03 18:31:31.872: W/DataManager(12540): at com.android.gallery3d.data.LocalSource.createMediaObject(LocalSource.java:106)
11-03 18:31:31.872: W/DataManager(12540): at com.android.gallery3d.data.DataManager.getMediaObject(DataManager.java:165)
11-03 18:31:31.872: W/DataManager(12540): at com.android.gallery3d.data.LocalSource.getDefaultSetOf(LocalSource.java:183)
11-03 18:31:31.872: W/DataManager(12540): at com.android.gallery3d.data.DataManager.getDefaultSetOf(DataManager.java:261)
11-03 18:31:31.872: W/DataManager(12540): at com.android.gallery3d.app.Gallery.startViewAction(Gallery.java:198)
11-03 18:31:31.872: W/DataManager(12540): at com.android.gallery3d.app.Gallery.initializeByIntent(Gallery.java:91)
11-03 18:31:31.872: W/DataManager(12540): at com.android.gallery3d.app.Gallery.onCreate(Gallery.java:68)
11-03 18:31:31.872: W/DataManager(12540): at android.app.Activity.performCreate(Activity.java:5008)
11-03 18:31:31.872: W/DataManager(12540): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-03 18:31:31.872: W/DataManager(12540): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-03 18:31:31.872: W/DataManager(12540): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
11-03 18:31:31.872: W/DataManager(12540): at android.app.ActivityThread.access$600(ActivityThread.java:143)
11-03 18:31:31.872: W/DataManager(12540): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
11-03 18:31:31.872: W/DataManager(12540): at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 18:31:31.872: W/DataManager(12540): at android.os.Looper.loop(Looper.java:137)
11-03 18:31:31.872: W/DataManager(12540): at android.app.ActivityThread.main(ActivityThread.java:4965)
11-03 18:31:31.872: W/DataManager(12540): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 18:31:31.872: W/DataManager(12540): at java.lang.reflect.Method.invoke(Method.java:511)
11-03 18:31:31.872: W/DataManager(12540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-03 18:31:31.872: W/DataManager(12540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
11-03 18:31:31.872: W/DataManager(12540): at dalvik.system.NativeStart.main(Native Method)
11-03 18:31:31.872: V/StateManager(12540): startState class com.android.gallery3d.app.PhotoPage
11-03 18:31:31.911: V/NFC(12540): this device does not have NFC support
11-03 18:31:32.044: W/DecodeService(12540): java.lang.NullPointerException
11-03 18:31:32.044: W/DecodeService(12540): at java.io.File.fixSlashes(File.java:185)
11-03 18:31:32.044: W/DecodeService(12540): at java.io.File.<init>(File.java:134)
11-03 18:31:32.044: W/DecodeService(12540): at java.io.FileInputStream.<init>(FileInputStream.java:105)
11-03 18:31:32.044: W/DecodeService(12540): at com.android.gallery3d.data.DecodeUtils.decodeThumbnail(DecodeUtils.java:92)
11-03 18:31:32.044: W/DecodeService(12540): at com.android.gallery3d.data.LocalImage$LocalImageRequest.onDecodeOriginal(LocalImage.java:188)
11-03 18:31:32.044: W/DecodeService(12540): at com.android.gallery3d.data.ImageCacheRequest.run(ImageCacheRequest.java:74)
11-03 18:31:32.044: W/DecodeService(12540): at com.android.gallery3d.data.LocalImage$LocalImageRequest.run(LocalImage.java:154)
11-03 18:31:32.044: W/DecodeService(12540): at com.android.gallery3d.data.ImageCacheRequest.run(ImageCacheRequest.java:28)
11-03 18:31:32.044: W/DecodeService(12540): at com.android.gallery3d.util.ThreadPool$Worker.run(ThreadPool.java:124)
11-03 18:31:32.044: W/DecodeService(12540): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-03 18:31:32.044: W/DecodeService(12540): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-03 18:31:32.044: W/DecodeService(12540): at java.lang.Thread.run(Thread.java:856)
11-03 18:31:32.044: W/DecodeService(12540): at com.android.gallery3d.util.PriorityThreadFactory$1.run(PriorityThreadFactory.java:43)
11-03 18:31:32.044: W/ImageCacheRequest(12540): decode orig failed /local/image/item/11271,THUMB
私はグーグルで検索しましたが、この例外に関する情報は見つかりませんでした。
私はこの問題について本当に混乱しており、修正方法がわかりません。誰かがそれを修正する方法を知っていますか? ありがとう。