我正在开发一个应该连接到URL的应用程序,将图像检索为Bitmap,然后在基于PanoramaGL库的查看器中显示它。
问题在于,即使Log表示图像被捕获,显示仍然是空白的。
这是我用来检索位图的代码,也是我在How to load an ImageView by URL in Android?找到的非常详细的答案。 - 实际上我想直接阅读图像而不用缓存等等。
正如您将看到的大部分代码都来自在线教程,因为我刚刚开始开发。
PanoramaActivity.java(为了测试它可以用作MainActivity)
import java.io.InputStream;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.panoramagl.PLImage;
import com.panoramagl.PLSpherical2Panorama;
import com.panoramagl.PLView;
public class PanoramaActivity extends PLView {
Private static final String TAG = "+++++++++++++++";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.panorama_activity_main);
new DownloadImageTask(this).execute("http://www.pianetacellulare.it/UserFiles/image/Android/Jelly%20Bean/photo_sphere_esempio.jpg");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
PanoramaActivity pan;
public DownloadImageTask(PanoramaActivity act) {
this.pan = act;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
Log.d(TAG,"onPostExecute");
PLSpherical2Panorama panorama = new PLSpherical2Panorama();
panorama.setImage(new PLImage(result));
pan.setPanorama(panorama);
}
}
}
我已经创建了传递PanoramaActivity的构造函数,因为我认为应用程序一直流到onPostExecute方法,我需要找到一种显示图像的方法。 如果我错了或有更好的方法请告诉我。
我还发布了tests_activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PanoramaActivity" >
</RelativeLayout>
任何提示都表示赞赏,提前谢谢:)
更新。我使用ImageView而不是PLView尝试了相同的AsyncTask类,并且图像出来了,因此正确地完成了图像的下载..使用PanoramaGL有些不正确
答案 0 :(得分:1)
删除
setContentView(R.layout.panorama_activity_main);
它覆盖了图像。 - &GT;即使我不明白为什么会发生这种情况
最重要的是......请确保您链接的图片符合要求:
PanoramaGL仅支持尺寸为2的图像,例如2048x1024,1024x1024,1024x512,512x512,512x256,256x256,256x128。
答案 1 :(得分:0)
使用Image loader
private void loadPanorama()
{
try
{
final Context context = this.getApplicationContext();
PLIPanorama panorama = null;
//Lock panoramic view
this.setLocked(true);
Toast.makeText(context, "image loader start", Toast.LENGTH_LONG).show();
imageLoader.loadImage("http://104.245.38.221/www/200/000/005/2000000057042/f/z_0/00.jpg", new SimpleImageLoadingListener()
{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
cubicPanorama.setImage(new PLImage(loadedImage, false), PLCubeFaceOrientation.PLCubeFaceOrientationFront);
}
});
imageLoader.loadImage("http://104.245.38.221/www/200/000/005/2000000057042/b/z_0/00.jpg", new SimpleImageLoadingListener()
{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
cubicPanorama.setImage(new PLImage(loadedImage, false), PLCubeFaceOrientation.PLCubeFaceOrientationBack);
}
});
imageLoader.loadImage("http://104.245.38.221/www/200/000/005/2000000057042/l/z_0/00.jpg", new SimpleImageLoadingListener()
{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
cubicPanorama.setImage(new PLImage(loadedImage, false), PLCubeFaceOrientation.PLCubeFaceOrientationLeft);
}
});
imageLoader.loadImage("http://104.245.38.221/www/200/000/005/2000000057042/r/z_0/00.jpg", new SimpleImageLoadingListener()
{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
cubicPanorama.setImage(new PLImage(loadedImage, false), PLCubeFaceOrientation.PLCubeFaceOrientationRight);
}
});
imageLoader.loadImage("http://104.245.38.221/www/200/000/005/2000000057042/u/z_0/00.jpg", new SimpleImageLoadingListener()
{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
cubicPanorama.setImage(new PLImage(loadedImage, false), PLCubeFaceOrientation.PLCubeFaceOrientationUp);
}
});
imageLoader.loadImage("http://104.245.38.221/www/200/000/005/2000000057042/d/z_0/00.jpg", new SimpleImageLoadingListener()
{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
cubicPanorama.setImage(new PLImage(loadedImage, false), PLCubeFaceOrientation.PLCubeFaceOrientationDown);
}
});
panorama = cubicPanorama;
Toast.makeText(context, "image loader end", Toast.LENGTH_LONG).show();
if(panorama != null)
{
//Set camera rotation
panorama.getCamera().lookAt(0.0f, 170.0f);
//Add a hotspot
//panorama.addHotspot(new PLHotspot(1, new PLImage(PLUtils.getBitmap(context, R.raw.hotspot), false), 0.0f, 170.0f, 0.05f, 0.05f));
//Reset view
this.reset();
//Load panorama
this.startTransition(new PLTransitionBlend(2.0f), panorama); //or use this.setPanorama(panorama);
}
//Unlock panoramic view
this.setLocked(false);
}
catch(Throwable e)
{
Toast.makeText(this.getApplicationContext(), "Error: " + e, Toast.LENGTH_SHORT).show();
}
}
如果出现黑屏,您可以覆盖
@Override
public void onLoadingStarted(String imageUri, View view) {
if (frontBitamp != null) {
cubicPanorama.setImage(new PLImage(frontBitamp, false),
PLCubeFaceOrientation.PLCubeFaceOrientationFront);
}};
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
cubicPanorama.setImage(new PLImage(loadedImage, false),
PLCubeFaceOrientation.PLCubeFaceOrientationFront);
frontBitamp = loadedImage;}