我一直在努力,因为SD卡中的位图没有显示在列表视图中。主要问题是在hashmap(map.put(TAG_ICON, bm)
)中包含位图,以便可以在Listview中填充它。我还打印了位图值android.graphics.Bitmap@41c4c650
,并显示位图宽度:256
。我只是希望图像包含在下面的列表视图中。希望听到你建议的决心。谢谢。 Noob在这里。
logcat的:
resolveUri failed on bad bitmap uri: android.graphics.Bitmap@41c15540
06-26 16:39:56.559: I/System.out(2312): resolveUri failed on bad bitmap uri:
06-26 16:45:48.989: D/Bitmap not Null(2798): android.graphics.Bitmap@41c4c650
爪哇
//In the Main Activity just fetch all the data from MYSql
//Then save it to the database
//After saving, then do an intent to display the menu
public class MainActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
Button bMain;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, ?>> productsList;
// url to get all products list
private static String url_all_products = "";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";
private static final String TAG_IMAGE_NAME = "imageName";
private static final String TAG_ICON=null;
// products JSONArray
JSONArray products = null;
Bitmap bm =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1*; 1.5*;2*;3;
//Identify the screen resolution
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
setContentView(R.layout.activity_main3);
Toast.makeText(this, "Large screen: "+screenSize,Toast.LENGTH_LONG).show();
File folder = new File(Environment.getExternalStorageDirectory() + "/FinalTestforDatabase");
boolean success1 = true;
if (!folder.exists()) {
success1 = folder.mkdir();
}
if (success1) {
Toast.makeText(MainActivity.this, "Success: ",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Fail ",Toast.LENGTH_LONG).show();
}
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, ?>>();
//bMain = (Button) findViewById(R.id.main);
// Loading products in Background Thread
new LoadAllProducts().execute();
if(bm!=null)
{
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setImageBitmap(bm);
}
// Get listview
ListView lv = getListView();
LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(this, R.anim.animlistview);
lv.setLayoutAnimation(controller);
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
AlphaInActivity.class);
// sending pid to next activity
in.putExtra("TAG_PID", pid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
//Toast.makeText(this.getBaseContext(),pid,
//Toast.LENGTH_SHORT).show();
}
});
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
setContentView(R.layout.activity_main2);
Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show();
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
setContentView(R.layout.activity_main2);
Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
//Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
byte[] decodedString = Base64.decode(c.getString(TAG_IMAGE_NAME), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
String fileN = (i+1)+".jpg";
OutputStream fOut = null;
File file = new File(Environment.getExternalStorageDirectory() + "/FinalTestforDatabase/",fileN);
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
decodedByte.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
try {
fOut.flush();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
fOut.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, Object> map = new HashMap<String, Object>();
String fileName;
fileName = (i+1)+".jpg";
File f = new File(Environment.getExternalStorageDirectory()+ "/FinalTestforDatabase/", fileName);
FileInputStream is = null;
try {
is = new FileInputStream(f);
} catch (FileNotFoundException e) {
Log.d("error: ",String.format( "ShowPicture.java file[%s]Not Found",fileName));
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[2*1024];
bm= BitmapFactory.decodeStream(is, null, options);
if(bm==null)
{
Log.d("Bitmap", "Null Value");
}
else
{
;
Log.d("Bitmap not Null", bm.toString());
Log.d("Bitmap Width", ""+bm.getWidth());
}
//map.put(TAG_ICON, bm);
map.put(TAG_PID, name);
map.put(TAG_NAME, id);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
MainActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
private Bitmap getbitmap(byte[] bytesArray) {
try{
byte[] decodedString2 = Base64.decode(bytesArray, Base64.DEFAULT);
Bitmap decodedByte2 = BitmapFactory.decodeByteArray(decodedString2, 0,decodedString2.length);
return decodedByte2;
}catch(Exception e){e.toString();}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, productsList,
R.layout.list_item, new String[] {TAG_ICON, TAG_PID,
TAG_NAME},
new int[] {R.id.list_image, R.id.pid, R.id.name });
//updating listview R.id.list_image, ,
setListAdapter(adapter);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class MyViewBinder implements ViewBinder {
public boolean setViewValue(View view, Object data, String textRepresentation) {
if ((view instanceof ImageView) & (data instanceof Bitmap)) {
ImageView iv = (ImageView) view;
Bitmap bm = (Bitmap) data;
iv.setImageBitmap(bm);
return true;
}
return false;
}
}
}
答案 0 :(得分:1)
使用
adapter.setViewBinder(new MyViewBinder());
public class MyViewBinder implements ViewBinder {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if ((view instanceof ImageView) & (data instanceof Bitmap)) {
ImageView iv = (ImageView) view;
Bitmap bm = (Bitmap) data;
iv.setImageBitmap(bm);
return true;
}
return false;
}
}