我创建了应用,允许我在网络上传图片。我通过PHP调整大小,然后我尝试将这些图片加载到另一个应用程序中......
我有下载代码的代码:
public Bitmap getBitmapFromURL(String src) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
然后我有AsyncTask,我下载所有图片: (我试图将图片组织成两排)
public class loadPhotos extends AsyncTask<String,Object,Void>{
ProgressBar pb;
ImageView img;
TableRow.LayoutParams paramsImage;
List<String> urlsOfImages; //gained from database
TableLayout tl;
TableRow tr;
LayoutParams lp;
int numberOfPhotos=0;
@Override
protected void onPreExecute(){
urlsOfImages = new ArrayList<String>();
allUserPhotoBitmaps.clear(); // saved bitmaps
pb = (ProgressBar) findViewById(R.id.profile_photos_progress_bar);
pb.setVisibility(View.VISIBLE);
tl = (TableLayout) findViewById(R.id.profile_photos_table_layout);
tl.removeAllViews(); //clear all photos before load new one
tr = new TableRow(Profile.this);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(lp);
}
@Override
protected Void doInBackground(String... arg) {
allUserPhotoInformations = myDb.getUrlsOfUserImages(arg[0],arg[1],arg[2]); //This returns List<HashMap<String,String>> , where I'm storing information from web database like : URL, Country, City, Latitude, Longitude etc...
Log.w("AllUserPhotoInformations","SIZE: "+ allUserPhotoInformations.size()); //Testing if size is OK
for (int i=0 ; i < allUserPhotoInformations.size() ; i++){
Bitmap bm = getBitmapFromURL(allUserPhotoInformations.get(i).get("file_path")); //getting bitmap from function posted above
allUserPhotoBitmaps.add(bm); //saving bitmap to List<Bitmap>
publishProgress(bm,i); // Publishing progress with Object bitmap and index
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Object... arg){
img = new ImageView(Profile.this);
paramsImage = new TableRow.LayoutParams(((int)(screenWidth/2))-30, ((int)(screenWidth/2))-30);
paramsImage.setMargins(10,10,0,0);
img.setLayoutParams(paramsImage);
img.setImageBitmap((Bitmap) arg[0]);
img.setVisibility(View.VISIBLE);
img.setOnClickListener(new ImageListener((Integer)arg[1])); //here I'm passing the index to onClick listener.
tr.addView(img);
if ( numberOfPhotos==1 ){
// If there's already two photos in tableRow, add the TableRow into TableLayout with these two pictures
try{
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}catch(Exception e){
e.printStackTrace();
}
// And create new tableRow
tr = new TableRow(Profile.this);
tr.setLayoutParams(lp);
numberOfPhotos--;
}
else {
numberOfPhotos++;
}
}
@Override
protected void onPostExecute(Void arg){
pb.setVisibility(View.GONE);
userPhotos.setText("uploaded: "+allUserPhotoBitmaps.size()+" photos");
if ( numberOfPhotos==1 ){
//If there was odd number of pictures, add the last into tableLayout too
try{
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}catch(Exception e){
e.printStackTrace();
}
/* Vytvor novej radek */
tr = new TableRow(Profile.this);
tr.setLayoutParams(lp);
}
}
}
有时它会显示所有图片,有时会加载其中两个,然后应用程序重新启动,有时会立即重新启动...
最后......有OnClick方法:
public class ImageListener implements OnClickListener{
int index;
Bitmap bm;
byte[] byteArray;
public ImageListener(int index) {
this.index = index;
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
allUserPhotoBitmaps.get(index).compress(Bitmap.CompressFormat.JPEG, 60, stream);
byteArray = stream.toByteArray();
}catch(Exception e){
e.printStackTrace();
}
//All log bellow contains no error
Log.w("bitmap",""+byteArray);
Log.w("country",allUserPhotoInformations.get(index).get("country"));
Log.w("city",allUserPhotoInformations.get(index).get("city"));
Log.w("latitude",allUserPhotoInformations.get(index).get("latitude"));
Log.w("longitude",allUserPhotoInformations.get(index).get("longitude"));
Log.w("description",allUserPhotoInformations.get(index).get("description"));
Log.w("category",allUserPhotoInformations.get(index).get("category"));
Log.w("date",allUserPhotoInformations.get(index).get("date"));
}
@Override
public void onClick(View v){
try {
Intent i = new Intent(getApplicationContext(), ShowPhotoDetails.class);
Log.w("onCLICK","1"); //ok
i.putExtra("bitmap",byteArray);
Log.w("onCLICK","2"); //ok
i.putExtra("country",allUserPhotoInformations.get(index).get("country"));
i.putExtra("city",allUserPhotoInformations.get(index).get("city"));
i.putExtra("latitude",allUserPhotoInformations.get(index).get("latitude"));
i.putExtra("longitude",allUserPhotoInformations.get(index).get("longitude"));
i.putExtra("description",allUserPhotoInformations.get(index).get("description"));
i.putExtra("category",allUserPhotoInformations.get(index).get("category"));
i.putExtra("date",allUserPhotoInformations.get(index).get("date"));
Log.w("onCLICK","3"); // ok
startActivity(i);
}catch(Exception e){
e.printStackTrace();
Toast.makeText(Profile.this,"Error showing image",Toast.LENGTH_SHORT).show();
}
}
};
我的问题是,当所有图片都正确显示(在其中一个案例中)并且我先点击时,根本没有问题......但是当我尝试打开例如最后一个时。 ..所有日志(&#34; onCLICK&#34;,&#34; 1&#34;;&#34; onClick&#34;,&#34; 2&#34;;&#34; onClick&#34;, &#34; 3&#34;)将可见,但应用程序在此之后重新启动...
然后是ShowPhotoDetails.class,里面有intentExtras:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.show_photo_details);
Log.w("SHOW_PHOTO_DETAILS","CREATED"); // Not OK
}
是的,是的!我已经宣布了Manifest中的所有活动,我也拥有所有权限......
答案 0 :(得分:0)
试试这个
@Override
public void onClick(View v){
try {
Intent i = new Intent(getBaseContext(), ShowPhotoDetails.class);
Log.w("onCLICK","1"); //ok
i.putExtra("bitmap",byteArray);
Log.w("onCLICK","2"); //ok
i.putExtra("country",allUserPhotoInformations.get(index).get("country"));
i.putExtra("city",allUserPhotoInformations.get(index).get("city"));
i.putExtra("latitude",allUserPhotoInformations.get(index).get("latitude"));
i.putExtra("longitude",allUserPhotoInformations.get(index).get("longitude"));
i.putExtra("description",allUserPhotoInformations.get(index).get("description"));
i.putExtra("category",allUserPhotoInformations.get(index).get("category"));
i.putExtra("date",allUserPhotoInformations.get(index).get("date"));
Log.w("onCLICK","3"); // ok
startActivity(i);
}catch(Exception e){
e.printStackTrace();
Toast.makeText(Profile.this,"Error showing image",Toast.LENGTH_SHORT).show();
}
}
将 getApplicationContext()更改为 getBaseContext()
答案 1 :(得分:0)
解决了!!!这是因为图像分辨率......
width = (screenWidth)/myBitmap.getRowBytes();
if ( myBitmap.getWidth() > myBitmap.getHeight() ){
height = ((myBitmap.getRowBytes()*width)/16)*11;
width = (height/11)*16;
}
else {
height = ((myBitmap.getRowBytes()*width)/11)*16;
width = (height/16)*10;
}
Bitmap bitmap = Bitmap.createScaledBitmap(myBitmap, (int)width, (int)height, true);