1

我的图片库有问题。我正在使用Dreddik 的 TouchImageGallery 库。我试图从 sd 卡文件夹加载图像。当我尝试从互联网 url 加载图像时它可以工作,但是当我从我的 sd 卡传递图像路径时它不会这样做。

我认为问题可能是文件路径,但我没有找到有关此的信息。我正在使用“原始粘贴”技术,但我认为我错了。

我把我的代码贴在下面:

主要活动:

public class GaleriaMainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_galeria_main);

        String []rutas = {"/storage/sdcard0/bola2.jpg", 
            "/storage/sdcard0/DCIM/Camera/1334835629645.jpg" };

        Intent i = new Intent(this, CargadoDesdeSd.class);
        i.putExtra("src", rutas);
        startActivity(i);

}

}

这是从主检索图像路径的类:

public class CargadoDesdeSd extends Activity {

private String[] imgPaths;
private GalleryViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cargado_desde_sd);
imgPaths = getIntent().getExtras().getStringArray("src");
Log.d("debug", imgPaths[0]);

String []urls = getIntent().getExtras().getStringArray("src");
//  String []urls = {
//                "http://www.holafamilias.com/wp-content/uploads/2010/11/hola-11.jpg",
//                "http://cs407831.userapi.com/v4078f31207/18fe/4Tz8av5Hlvo.jpg",
//                "http://cs407831.userapi.com/v407831207/1906/oxoP6URjFtA.jpg",
//                "http://cs407831.userapi.com/v407831207/190e/2Sz9A774hUc.jpg",
//                "http://cs407831.userapi.com/v407831207/1916/Ua52RjnKqjk.jpg",
//                "http://cs407831.userapi.com/v407831207/191e/QEQE83Ok0lQ.jpg"};

    List<String> items = new ArrayList<String>();
    Collections.addAll(items, urls);

    UrlPagerAdapter pagerAdapter = new UrlPagerAdapter(this, items);
    pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
    {
        @Override
        public void onItemChange(int currentPosition)
        {
            Log.d("debug","Current item is " + currentPosition);
        }
    });

    mViewPager = (GalleryViewPager)findViewById(R.id.viewer);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setAdapter(pagerAdapter);
}
}

有谁知道为什么会这样?

非常感谢!!!:)

编辑 这就是 logcat 说的:

05-30 12:02:37.085: W/System.err(24489): java.net.MalformedURLException: Protocol not found: /storage/sdcard0/DCIM/Camera/1338473970511.jpg
05-30 12:02:37.085: W/System.err(24489):    at java.net.URL.<init>(URL.java:178)
05-30 12:02:37.085: W/System.err(24489):    at java.net.URL.<init>(URL.java:127)
05-30 12:02:37.085: W/System.err(24489):    at ru.truba.touchgallery.TouchView.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:87)
05-30 12:02:37.090: W/System.err(24489):    at ru.truba.touchgallery.TouchView.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:1)
05-30 12:02:37.100: W/System.err(24489):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-30 12:02:37.100: W/System.err(24489):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-30 12:02:37.100: W/System.err(24489):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-30 12:02:37.100: W/System.err(24489):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-30 12:02:37.100: W/System.err(24489):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-30 12:02:37.100: W/System.err(24489):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-30 12:02:37.100: W/System.err(24489):    at java.lang.Thread.run(Thread.java:856)
05-30 12:02:37.100: D/STATUSBAR-NotificationService(2119): enqueueToast()
05-30 12:02:37.105: W/System.err(24489): java.net.MalformedURLException: Protocol not found: /storage/sdcard0/DCIM/Camera/1334835629645.jpg
05-30 12:02:37.105: W/System.err(24489):    at java.net.URL.<init>(URL.java:178)
05-30 12:02:37.105: W/System.err(24489):    at java.net.URL.<init>(URL.java:127)
05-30 12:02:37.105: W/System.err(24489):    at ru.truba.touchgallery.TouchView.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:87)
05-30 12:02:37.105: W/System.err(24489):    at ru.truba.touchgallery.TouchView.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:1)
05-30 12:02:37.105: W/System.err(24489):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-30 12:02:37.105: W/System.err(24489):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-30 12:02:37.105: W/System.err(24489):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-30 12:02:37.105: W/System.err(24489):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-30 12:02:37.105: W/System.err(24489):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-30 12:02:37.105: W/System.err(24489):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-30 12:02:37.105: W/System.err(24489):    at java.lang.Thread.run(Thread.java:856)
05-30 12:02:37.105: W/System.err(24489): java.net.MalformedURLException: Protocol not found: /storage/sdcard0/DCIM/Camera/bola2.jpg
05-30 12:02:37.105: W/System.err(24489):    at java.net.URL.<init>(URL.java:178)
05-30 12:02:37.105: W/System.err(24489):    at java.net.URL.<init>(URL.java:127)
05-30 12:02:37.105: W/System.err(24489):    at ru.truba.touchgallery.TouchView.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:87)
05-30 12:02:37.105: W/System.err(24489):    at ru.truba.touchgallery.TouchView.UrlTouchImageView$ImageLoadTask.doInBackground(UrlTouchImageView.java:1)
05-30 12:02:37.105: W/System.err(24489):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-30 12:02:37.105: W/System.err(24489):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-30 12:02:37.105: W/System.err(24489):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-30 12:02:37.105: W/System.err(24489):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-30 12:02:37.105: W/System.err(24489):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-30 12:02:37.105: W/System.err(24489):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-30 12:02:37.105: W/System.err(24489):    at java.lang.Thread.run(Thread.java:856)

编辑: 我意识到我正在尝试从 URL 图像加载器类加载 SD 卡图像。我已经重新定位它,现在这是我的主要活动代码如下所示:

    /*
*Fields
*/

final String TAG= "debug";
private GalleryViewPager mViewPager;
private String[] imgPaths;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgPaths = getIntent().getExtras().getStringArray("src");
Log.d(TAG, imgPaths[0]);
List<String> items = new ArrayList<String>();
int readLimit = 16 * 1024;
try {
for (String filename : imgPaths) 
{
if (filename.matches(".+\\.jpg")) 
{
InputStream is = null;
try {
    is = new BufferedInputStream(new FileInputStream(new File(filename, "")), readLimit);
    is.mark(readLimit); 
    } catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }
copy(is, new File(filename) );
items.add(filename);
}
}
} catch (IOException e) 
        {
            e.printStackTrace();
        }

FilePagerAdapter pagerAdapter = new FilePagerAdapter(this, items);
pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
{
    @Override
    public void onItemChange(int currentPosition)
    {
        Log.d(TAG, "Current item is " + currentPosition);
    }
});

mViewPager = (GalleryViewPager)findViewById(R.id.viewer);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setAdapter(pagerAdapter);


public void copy(InputStream in, File dst) throws IOException 
{
    OutputStream out = new FileOutputStream(dst);
    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}
}

我没有运行时错误,但它显示默认错误图像并且不显示我想要的图像:\

4

2 回答 2

1

不要像这样使用你的 SD 卡路径

用这个

File sdCard = Environment.getExternalStorageDirectory();
String folder = sdCard.getAbsolutePath() + "/YourFolder" ;

根据你的评论

path = sdCard.getAbsolutePath() + "/DCIM/Camera/";

编辑:根据评论中提供的链接

FilePagerAdapter pagerAdapter = new FilePagerAdapter(this, items);
    pagerAdapter.setOnItemChangeListener(new OnItemChangeListener()
    {
        @Override
        public void onItemChange(int currentPosition)
        {
            Toast.makeText(GalleryFileActivity.this, "Current item is " + currentPosition, Toast.LENGTH_SHORT).show();
        }
    });

    mViewPager = (GalleryViewPager)findViewById(R.id.viewer);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setAdapter(pagerAdapter);

有关更多信息,请访问此链接,其中讨论了如何使用基于本地文件的图像而不是基于 Web 的图像。

于 2013-05-30T09:09:41.020 回答
0

使用此方法:

Environment.getExternalStorageDirectory();

获取 sd 文件夹的根路径。

于 2013-05-30T09:09:23.480 回答