2016-08-04 92 views
0

我保持一個學校項目,處理一個充滿圖像託管在服務器上的ListView。
當有人從列表中選擇一張圖片時,在另一種佈局中顯示爲「原始」尺寸,甚至在這裏一切都很順利。
但也詢問圖像顯示何時可以選擇將其保存到SD卡,這是我不能做的部分。Android Studio:將圖像保存在SD卡中

這是我做的代碼:

public class MainActivity extends AppCompatActivity { 

private ListView listView; 
private ProgressDialog progressDialog; 
ArrayList asuntos=new ArrayList(); 
ArrayList imagen=new ArrayList(); 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolBar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolBar); 
    listView=(ListView) findViewById(R.id.list); 
    progressDialog = new ProgressDialog(this); 
    progressDialog.setCancelable(false); 
    lista o=new lista(); 
    o.obtenerAvisos(); 
} 

public class lista { 

    public void obtenerAvisos() { 

     asuntos.clear(); 
     imagen.clear(); 

     String tag_string_req = "req_data"; 

     progressDialog.setMessage("Conectando..."); 
     showDialog(); 

     StringRequest strReq = new StringRequest(Request.Method.POST, AppURLs.URL, new Response.Listener<String>() { 

      public void onResponse(String response) { 

       hideDialog(); 

       try { 

        JSONArray jsonArray = new JSONArray(response); 

        for (int i = 0; i < jsonArray.length(); i++) { 
         asuntos.add(jsonArray.getJSONObject(i).getString("asunto")); 
         imagen.add(jsonArray.getJSONObject(i).getString("publicacion")); 
        } 

        listView.setAdapter(new ImagenAdaptador(getApplicationContext())); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, new Response.ErrorListener() { 

      public void onErrorResponse(VolleyError error) { 

       Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); 
       hideDialog(); 
      } 
     }) { 

      protected Map<String, String> getParams() { 

       Map<String, String> params = new HashMap<String, String>(); 
       params.put("tag", "data"); 
       return params; 
      } 

     }; 
     AppController.getInstance().addToRequestQueue(strReq, tag_string_req); 
    } 
} 

private void showDialog() { 

    if (!progressDialog.isShowing()) 
     progressDialog.show(); 
} 

private void hideDialog() { 

    if (progressDialog.isShowing()) 
     progressDialog.dismiss(); 
} 

public class ImagenAdaptador extends BaseAdapter { 

    Context ctx; 
    LayoutInflater layoutInflater; 
    SmartImageView smartImageView; 
    TextView tvasunto; 

    public ImagenAdaptador(Context applicationContext) { 

     this.ctx=applicationContext; 
     layoutInflater=(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return imagen.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 


    public long getItemId(int position) { 
     return position; 
    } 


    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewGroup viewGroup=(ViewGroup) layoutInflater.inflate(R.layout.activity_main_items,null); 
     smartImageView=(SmartImageView)viewGroup.findViewById(R.id.imagen1); 
     tvasunto=(TextView) viewGroup.findViewById(R.id.tvAsunto); 
     final String urlfinal="http://192.168.43.45/InfoTec/publicaciones/"+imagen.get(position).toString(); 
     Rect rect=new Rect(smartImageView.getLeft(), smartImageView.getTop(), smartImageView.getRight(), smartImageView.getBottom()); 
     smartImageView.setImageUrl(urlfinal, rect); 
     tvasunto.setText(asuntos.get(position).toString()); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Intent intent = new Intent(MainActivity.this, Visor.class); 
       intent.putExtra("arg", asuntos.get(position).toString()); 
       intent.putExtra("arg2",imagen.get(position).toString()); 
       startActivity(intent); 
      } 
     }); 
     return viewGroup; 
    } 
} 
} 

這部分作品,但這個類的問題是:

public class Visor extends AppCompatActivity { 

TextView tvasunto2; 
SmartImageView smartImageView2; 
Button descarga; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_visor); 
    String original=getIntent().getStringExtra("arg"); 
    String imgOriginal=getIntent().getStringExtra("arg2"); 
    tvasunto2=(TextView) findViewById(R.id.tvAsunto2); 
    smartImageView2=(SmartImageView) findViewById(R.id.imagen2); 
    descarga=(Button) this.findViewById(R.id.button); 

    tvasunto2.setText(original); 
    String url="http://192.168.43.45/InfoTec/publicaciones/"+imgOriginal; 
    Rect rect=new Rect(smartImageView2.getLeft(), smartImageView2.getTop(), smartImageView2.getRight(), smartImageView2.getBottom()); 
    smartImageView2.setImageUrl(url,rect); 

    descarga.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     } 
    }); 
} 
} 

如果顯示所選擇的圖像,但不是現在如何處理數據在下載按鈕的情況下。
會是什麼解決方案?

回答

0

首先,你需要得到你的位圖。你已經可以把它作爲一個對象位圖,或者你可以嘗試從ImageView的得到它,例如:

BitmapDrawable drawable = (BitmapDrawable) mImageView1.getDrawable(); 
Bitmap bitmap = drawable.getBitmap(); 

然後,你必須讓從SD卡目錄(File對象),如:

File sdCardDirectory = Environment.getExternalStorageDirectory(); 

接下來,建立特定的文件存儲圖像:

File image = new File(sdCardDirectory, "test.png"); 

之後,你只需要編寫位圖得益於其方法壓縮,如:

boolean success = false; 

// Encode the file as a PNG image. 
FileOutputStream outStream; 
try { 

    outStream = new FileOutputStream(image); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
    /* 100 to keep full quality of the image */ 

    outStream.flush(); 
    outStream.close(); 
    success = true; 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

最後,如果需要處理布爾結果。如:

if (success) { 
    Toast.makeText(getApplicationContext(), "Image saved with success", 
      Toast.LENGTH_LONG).show(); 
} else { 
    Toast.makeText(getApplicationContext(), 
      "Error during image saving", Toast.LENGTH_LONG).show(); 
} 

不要忘記添加以下權限在您的清單:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

完美的作品非常感謝你。當然,我必須閱讀更多關於Android中使用的資源的信息,我試圖找到另一種無關的方式。 – PromptShadow

+0

歡迎您:) – LychmanIT